I am new to IOS development. In windows phone development we use to define a style(appearance e.g) of the button control in app Global place and just use that style anywhere on any button in the app by assigning the style property.
Is there anything similar in IOS XCode? I know I can customize the button in the property and appearance windows in XCode. But in this way I have to give style to each and every similar button in the app. and if there is any change I have to change it everywhere. What IOS developers do in these kind situations?
If you want to style all buttons in your app in a standard way, then you would style the appearance proxy for UIButton
. You get one by calling the class method UIButton.appearance()
, and then styling the returned value the way you want your buttons to look. You can also do a more fine-grained appearance by calling methods like UIButton.appearanceWhenContainedIn()
.
Create a UIButton subclass. For example for creating a UIButton with rounded corners and a border:
Header file
#import <Foundation/Foundation.h>
@interface XYRoundedCornerButton : UIButton
@end
Implementation file
...import headers etc...
@implementation XYRoundedCornerButton
- (void)awakeFromNib {
[super awakeFromNib];
self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderRadius = 1.0f;
self.layer.borderColor = UIColor.redColor;
}
@end
If you are using storyboard, You don't need to add any code in class. Just drag one button and set all the properties you want, whether background colour, corner radius etc, whatever you want. And then either copy it and paste wherever you want or select the button, ALT+DRAG it .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With