我有一个按钮,它在选定状态和正常状态下有两个不同的文本。当我通过编程更改按钮的状态时,按钮没有被调整大小,所以文本无法正确显示。使用自动布局,哪种方式是最好的呢?
我知道一种方法是将outlet设置为UIButton的宽度约束,并手动更改,但我正在寻找更好的方法。
我有一个按钮,它在选定状态和正常状态下有两个不同的文本。当我通过编程更改按钮的状态时,按钮没有被调整大小,所以文本无法正确显示。使用自动布局,哪种方式是最好的呢?
我知道一种方法是将outlet设置为UIButton的宽度约束,并手动更改,但我正在寻找更好的方法。
只需使用[button invalidateIntrinsicContentSize];
,它就能像您期望的那样执行。
使用自动布局来缩小/扩展UIButton的宽度的另一种方法是添加一个宽度约束,然后将宽度约束的优先级改为小于Content Hugging Priority和Content Compression Resistance Priority。
如果我们只想根据按钮标题扩展按钮,则可以将宽度约束设置为最小值,并将宽度约束的优先级设置为小于Content Compression Resistance Priority但大于Content Hugging Priority。
在上述情况下,我们只需要
btn.setTitle("SomeTitle", for: .normal)
您可以像这样使用默认按钮:
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)]];
@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
当您创建约束时,请将其分配给变量。
@property (nonatomic, strong)NSLayoutConstraint *viewConstraintTop;
self.viewConstraintTop = [Your Constraint];//Initial state
在按钮点击时检查它是否被选中。移除约束并重新应用适当的约束。
[self.viewConstraintTop autoRemove];
self.viewConstraintTop = [Your Constraint];
试试这个
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);