UIButton dynamic width using autostart

I have a button that has two different texts in the selected state and in the normal state. When I change the state of a button programmatically, the button does not change, so the text does not display correctly, what is the best way to do this with autostart?

I know that one way sets the output to the UIButton width limit and changes it manually, but I'm looking for a better way

+6
source share
4 answers

Just by reporting [button invalidateIntrinsicContentSize]; He must do what you expect from him.

+3
source

You can use the default button as follows:

 UIButton* button = [UIButton buttonWithType:UIButtonTypeSystem]; [button setTranslatesAutoresizingMaskIntoConstraints:NO]; [button setTitle:@"Normal" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside]; [button setTitle:@"Selected" forState:UIControlStateSelected]; [self.view addSubview:button]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[button]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[button]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(button)]]; 

If you use a custom button, in the simplest way I can name the UIButton subclass and add your custom UILabel inside. Here is my short code that I have in mind:

 @interface CustomButton : UIButton { NSString* _normalTitle; NSString* _selectedTitle; } @property UILabel* customLabel; @end @implementation CustomButton @synthesize customLabel=_customLabel; - (instancetype)init; { self = [super init]; if ( self ) { [self setBackgroundColor:[UIColor greenColor]]; _customLabel = [UILabel new]; [_customLabel setTextColor:[UIColor whiteColor]]; [_customLabel setTranslatesAutoresizingMaskIntoConstraints:NO]; [self addSubview:_customLabel]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_customLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_customLabel)]]; [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_customLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_customLabel)]]; } return self; } - (void)setTitle:(NSString *)title forState:(UIControlState)state; { if ( state == UIControlStateNormal ) { _normalTitle = title; } else if ( state == UIControlStateSelected ) { _selectedTitle = title; } [self setSelected:[self isSelected]]; } - (void)setSelected:(BOOL)selected; { [super setSelected:selected]; if ( selected ) { [_customLabel setText:_selectedTitle]; } else { [_customLabel setText:_normalTitle]; } } @end 
0
source

When creating constraints, assign it to variables

 @property (nonatomic, strong)NSLayoutConstraint *viewConstraintTop; self.viewConstraintTop = [Your Constraint];//Initial state 

Click the "Disable" button, check if it is selected or not. Remove the constraints and reapply the appropriate constraints.

 [self.viewConstraintTop autoRemove]; self.viewConstraintTop = [Your Constraint]; 
0
source

try it

 CGSize maxSize = CGSizeMake(btn.bounds.size.width, CGFLOAT_MAX); CGSize textSize1 = [btn.titleLabel.text sizeWithFont:btn.titleLabel.font constrainedToSize:maxSize]; btn.frame=CGSizeMake(10,10,textSize1.width,30); 
-2
source

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


All Articles