使用autolayout的UIButton动态宽度

时间:2014-10-30 11:29:28

标签: ios objective-c ios7 uibutton autolayout

我有一个按钮,其中有两个不同的文本处于选中状态和正常状态,当我改变按钮的状态时,编程按钮没有调整大小,因此文本没有正确显示,这是使用autolayout执行此操作的最佳方法吗?

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

5 个答案:

答案 0 :(得分:3)

只要告诉sectionTitles = [[NSMutableArray alloc] initWithCapacity:sectionTitles.count]; sectionTitles = [NSMutableArray new]; sectionTitles = [NSMutableArray array]; sectionTitles = [NSMutableArray arrayWithCapacity:sectionTitles.count]; ,它应该按照您的期望去做。

答案 1 :(得分: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

答案 2 :(得分:0)

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

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

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

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

答案 3 :(得分:0)

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

如果只希望基于按钮的标题展开按钮,则可以将宽度限制设置为最小值,并将宽度限制的优先级设置为小于 Content Compression Resistance Priority 但大于内容拥抱优先级

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

getAllAddedItems(addedItems) {

  addedItems.forEach(item => {
     this.product.forEach(({products}) => {
        products.forEach(({name, items}) => {
           const index = items.findIndex(i => i.functional_id === item.functional_id);
           if (index > -1) item.name = name;
        })
     })
  })

  console.log(addedItems);
}
代码中的

和自动布局将负责其余的工作。

随附的是屏幕截图enter image description here

答案 4 :(得分:-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);