Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set UIButton type in UIButton Subclass

Tags:

I am subclassing the UIButton, what i want is to set the button type to Round Rect.

Button.h

@interface Button : UIButton {}     - (void)initialize; @end 

Button.m

@implementation Button  - (id)initWithFrame:(CGRect)frame {     self = [super initWithFrame:frame];     if (self) {         [self initialize];     }     return self; }   -(id)initWithCoder:(NSCoder *)aDecoder {     self = [super initWithCoder:aDecoder];     if(self){         [self initialize];     }     return self; }  - (void)initialize {     self.titleLabel.font = [UIFont systemFontOfSize:20];     self.titleLabel.textColor = [UIColor redColor];     self.titleLabel.textAlignment = UITextAlignmentCenter;    //[UIButton buttonWithType:UIButtonTypeRoundedRect]; }  @end 

Here i tried [UIButton buttonWithType:UIButtonTypeRoundedRect] but it doesn't work. Can anyone suggest how to make it work?

I know in many previous post it has been said that Subclassing UIButton is not recommended, but the fact that in Developer's Docs there is no mention about NOT subclassing it.

like image 437
Haris Hussain Avatar asked Apr 23 '12 09:04

Haris Hussain


People also ask

What is UIButton Swift?

A control that executes your custom code in response to user interactions.


2 Answers

UIButton subclass in Swift

*The following code works in Swift 3 and above.

You cannot set the buttonType of a UIButton subclass by design. It is automatically set to custom which means you get a plain appearance and behavior as a starting point.

If you want to reuse code that sets the button's appearance, you can do this without subclassing. One approach is by providing factory methods that create a UIButton and set visual properties.

Example factory method to avoid subclassing

extension UIButton {     static func createStandardButton() -> UIButton {         let button = UIButton(type: UIButtonType.system)         button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)         button.setTitleColor(UIColor.black, for: .normal)         button.setTitleColor(UIColor.gray, for: .highlighted)         return button     } }  let button = UIButton.createStandardButton() 

I avoid subclassing UIButton when other customization techniques suffice. The factory method example is one option. There are other options including the UIAppearance API, etc.

Sometimes there are customization needs that require subclassing. For example, I've created UIButton subclasses to take fine control over how the button animates in response to touch events or to have the button call a predefined delegate or closure when tapped (as opposed to assigning a #selector to each instance).

The following is a basic UIButton subclass example as a starting point.

Example UIButton Subclass

internal class CustomButton: UIButton {      init() {         // The frame can be set outside of the initializer. Default to zero.         super.init(frame: CGRect.zero)         initialize()     }      required init?(coder aDecoder: NSCoder) {         // Called when instantiating from storyboard or nib         super.init(coder: aDecoder)         initialize()     }      func initialize() {         print("Execute common initialization code")     } }  let button = CustomButton() print(button.buttonType == .custom)  // true 

 

A note about UIButtonType

Since the question was asked in 2012, UIButtonType.roundedRect has been deprecated. The header file comments say to use UIButtonType.system instead.

The following is from UIButton.h converted to Swift in Xcode

public enum UIButtonType : Int {      case custom // no button type      @available(iOS 7.0, *)     case system // standard system button      case detailDisclosure     case infoLight     case infoDark     case contactAdd      public static var roundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead } 
like image 165
Mobile Dan Avatar answered Nov 20 '22 15:11

Mobile Dan


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)

like image 35
Caleb Avatar answered Nov 20 '22 15:11

Caleb