【问题标题】:UIButton dynamic width using autolayout使用自动布局的 UIButton 动态宽度
【发布时间】:2014-12-26 10:10:49
【问题描述】:

我有一个按钮,在选定状态和正常状态下有两个不同的文本,当我以编程方式更改按钮的状态时,按钮没有调整大小,因此文本无法正确显示,这是使用自动布局执行此操作的最佳方法?

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

【问题讨论】:

  • 已经读过但没有多大帮助:(
  • 在更改按钮的状态时-使按钮大小足够大,以便字符串可以轻松进入其中,但在此之前请将按钮的中心存储在变量中,然后使用适合大小的方法,然后再次提供相同的中心到按钮。
  • 谢谢,但我正在寻找更干净的解决方案 ;)
  • 您找到合适的解决方案了吗?我做了我能够使 UILabel 使用自动布局动态调整宽度的方法,但同样的方法不适用于 UIButton

标签: ios objective-c ios7 uibutton autolayout


【解决方案1】:

试试这个

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】:

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

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

【讨论】:

    【解决方案3】:

    在创建约束时将其分配给变量

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

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

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

    【讨论】:

      【解决方案4】:

      只需告诉[button invalidateIntrinsicContentSize];,它就应该按照您的期望去做。

      【讨论】:

      • 这拯救了我的一天 - 谢谢!
      【解决方案5】:

      使用自动布局缩小/扩展 UIButton 宽度的另一种方法是添加宽度约束,然后将宽度约束的优先级更改为小于 Content Hugging Priority内容抗压缩优先级

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

      以上情况我们只需要

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

      在代码中,自动布局将负责其余的工作。

      附上截图

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        • 1970-01-01
        • 2016-08-01
        相关资源
        最近更新 更多