how to set UIButton type in UIButton Subclass
You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:
Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)
Change UIButton type in subclass if button created from storyboard
I think it is not possible at runtime. Since it is a read-only method. I think you will get helpful questions from these links.
set UIButton's buttonType programmatically
Change UIButton type programatically
how to set UIButton type in UIButton Subclass
suclassing a UIButton of type .system
You can't override a convenience method and call super convenience method...
As an alternative you can do a static method that return a FormButton
of UIButtonType.system
type.
class FormButton: UIButton {
class func newButton() -> FormButton {
return FormButton.init(type: .system)
}
}
Use it like this
let button = FormButton.newButton()
how to set UIButton type in UIButton Subclass
You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:
Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)
how to set UIButton type in UIButton Subclass
You may find the discussion at CocoaBuilder's thread How to subclass UIButton? helpful, particularly Jack Nutting's suggestion to ignore the buttonType:
Note that this way the buttonType isn't explicitly set to anything,
which probably means that it's UIButtonTypeCustom. The Docs don't
seem to actually specify that, but since that's the 0 value in the
enum, that's likely what happens (and that seems to be the observable
behavior as well)
Subclassing UIButton with specific UIButtonType
Since UIButtonType
is a read-only property, it is not possible to do this.
Only two scenario to subclass UIButton
:
Create a subclass of UIbutton and create a public method to wrap it's initial method.
public class MyButton : UIButton
{
public static MyButton CreateButton()
{
return UIButton.FromType(UIButtonType.Custom) as MyButton;
}
}Usage:
MyButton button = MyButton.CreateButton();
You can only use it in code not designer in this way
Create a button from designer and rename its class
It will auto generate a subclass of UIButton named CustomButton in you app folder ,and you can assign it to other buttons in designer.
But as i mentioned above,
UIButtonType
is a read-only property , no way to change it once it's set.public partial class CustomButton : UIButton
{
public CustomButton (IntPtr handle) : base (handle)
{
this.ButtonType = UIButtonType.Custom; //incorrect , read-only
this.Font = UIFont.SystemFontOfSize(10); //correct, read-write
}
}RE: Change UIButton type in subclass if button created from storyboard
Swift UIButton Subclass and change color based on variable
Your isActive
property is written incorrectly. It should not be a computed property in the first place. Currently, the getter will just cause infinite recursion and the setter doesn't actually set anything.
The isActive
property should be a stored property with a didSet
property observer:
@IBInspectable
var isActive: Bool {
didSet {
}
}
Inside didSet
, you can just put the last part of commonInit
. The first part of commonInit
doesn't need to be run every time isActive
changes. I recommend you to extract that as a method called updateBorder
:
func updateBorder(isActive: Bool) {
if (isActive) {
self.tintColor = ACTIVE_COLOR
self.layer.borderColor = ACTIVE_COLOR.cgColor
} else {
self.tintColor = nil
self.layer.borderColor = UIColor(red:0.69, green:0.72, blue:0.77, alpha:1.0).cgColor
}
}
And then in didSet
, you can just call that:
updateBorder(isActive: isActive)
create uibutton subclass
You don’t really want to subclass UIButton
. It’s a class cluster, so individual instances will be something like UIRoundRectButton
or some other private Apple class. What are you trying to do that requires a subclass?
Related Topics
Odd Property Declaration Syntax Containing Angular Brackets <>
Ciimage Display Mtkview VS Glkview Performance
Google 400 Error: Invalid Request Custom Scheme Uris Are Not Allowed for 'Web' Client Type
What Does $0 Represent in Closures in Swift
Uiapplicationlaunchoptionsremotenotificationkey Not Getting Userinfo
How to Capture Taps Ignored by Wkwebview
How We Can Set the Light Content Style of Status Bar in iOS 9 for Whole Application
iOS 13 Set Uisearchtextfield Placeholder Color
Get the Expiration Date of a Provisioning Profile at Run-Time
Build Information in iOS Application (Date/Time App Was Built)
Chrome for iOS User Agent on iPad
Programmatically Sending an App to Background
Check If 3D Touch Is Supported and Enabled on the iOS9 Device
Cannot Get My Location to Show Up in Google Maps in iOS 8
Synchronizing Read/Write Access to an Instance Variable for High Performance in iOS