UIButtons styling - subclass?

I have four buttons that will appear the same on the same scene (using the Storyboard). This is a simple style that will require overriding a few defaults, but seems overly repetitive to set this for each individual button. I thought Id was creating a subclass, but many of the posts I read (especially on stackoverflow) warn about this for UIButton (and Ive madent have succeeded).

We just hope to get a general pointer to what is considered the best for this. Thanks for any advice.

+4
source share
2 answers

If you focus only on iOS 5, I highly recommend watching Session 114 - Customizing the Appearance of UIKit Controls, you need to register a video developer for the WWDC 2011 session.

It details the App-wide style.


I want to change the idea of ​​yujis: use category in UIButton to customize the button

.h.

@interface UIButton (MyStyling) -(void)configureMyButtonStyle; //other methods for more fine-grained control @end 

.m

 @implementation UIButton (MyStyling) -(void)configureMyButtonStyle { [self setBackgroundColor:[UIColor colorWithRed:…]]; [self setTitleColor: [UIColor colorWithRed:…] forState: UIControlStateNormal]; //… } @end 

Now you can call [aButton configureMyButtonStyle]

Of course, you can also analyze some parameters to distinguish between several styles.

 -(void)configureMyButtonForStyle:(NSInteger)style { if(style == 1){ //… } else if(style == 2) { //.. } else { //fallback style } } 

using:

 [aButton configureMyButtonForStyle:1]; 
+6
source

Subclassification here seems redundant. You could simply write a method that sets the properties the way you want in one of your controllers and call them on each UIButton .

Or you can always just tweak one button the way you want in IB, and then copy and paste three times.

+4
source

Source: https://habr.com/ru/post/1397898/


All Articles