使用自动布局实现UIButton的动态宽度

9

我有一个按钮,它在选定状态和正常状态下有两个不同的文本。当我通过编程更改按钮的状态时,按钮没有被调整大小,所以文本无法正确显示。使用自动布局,哪种方式是最好的呢?

我知道一种方法是将outlet设置为UIButton的宽度约束,并手动更改,但我正在寻找更好的方法。


1
https://dev59.com/I2855IYBdhLWcg3wz3zo - Alfa
已经阅读过了,但并没有太大帮助 :( - Abhishek
在改变按钮状态时,使按钮大小足够大,以便字符串可以轻松地放入其中,但在此之前,请将按钮中心存储在变量中,然后使用适合大小的方法,再次将相同的中心提供给按钮。 - Alfa
谢谢,但我正在寻找更加简洁的解决方案 ;) - Abhishek
你找到了合适的解决方案吗?我已经尽力使用自动布局使UILabel动态调整宽度,但是同样的方法在UIButton上不起作用。 - Kong Hantrakool
5个回答

4

只需使用[button invalidateIntrinsicContentSize];,它就能像您期望的那样执行。


2

使用自动布局来缩小/扩展UIButton的宽度的另一种方法是添加一个宽度约束,然后将宽度约束的优先级改为小于Content Hugging PriorityContent Compression Resistance Priority

如果我们只想根据按钮标题扩展按钮,则可以将宽度约束设置为最小值,并将宽度约束的优先级设置为小于Content Compression Resistance Priority但大于Content Hugging Priority

在上述情况下,我们只需要

btn.setTitle("SomeTitle", for: .normal)

在代码中添加布局,自动布局将处理其余部分。
附上一张截图enter image description here

0

您可以像这样使用默认按钮:

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)]];

如果您使用自定义按钮,则我能想到的最简单的方法是子类化UIButton并在其内部添加自定义UILabel。以下是我所指的简短代码:
@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

当您创建约束时,请将其分配给变量。

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

在按钮点击时检查它是否被选中。移除约束并重新应用适当的约束。

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

-2

试试这个

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
通常我们不在自动布局中设置框架。 - Abhishek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接