2014-02-18 33 views
2

战斗我有一个观点应该是这样的两个按钮与自动版式

---------------------------------- 
| [button 1]   [button 2] | 
---------------------------------- 

这一点,但我在其中一个或另一个将接管整个宽度的问题。我似乎无法找到正确的抗压缩/内容拥抱组合来获得我想要的。

我使用下面的可视布局代码:

H:|-(leftPadding)-[button1]-(>=middlePadding)-[button2]-(rightPadding)-| 
V:|-(topPadding)-[button1]-(bottomPadding)-| 
V:|-(topPadding)-[button2]-(bottomPadding)-| 

其中填充值是所有当前8.又,按钮不应重叠,所以第二按钮的宽度应当优先于所述第一的。

在应用中,标签可能会改变,所以我想是这样的:

---------------------------------- 
| [button 1] [some other button] | 
---------------------------------- 

或:

---------------------------------- 
| [some other button] [button 2] | 
---------------------------------- 

当我更新按钮上的文字,做我需要做的还要别的吗?

回答

2

对于左键(标题可能压缩),将压缩阻力和内容拥抱优先级保留为其默认设置。换句话说,什么都不做。

对于右键(标题不可压缩),增加其抗压优先级。默认的压缩阻力优先级是750.将其增加到751就足够了。

[button2 setContentCompressionResistancePriority:751 forAxis:UILayoutConstraintAxisHorizontal]; 

如果你在IB这样做:

左边的按钮(标题可以压缩): enter image description here

右边的按钮(标题不能被压缩):

enter image description here

更新: 用于定位按钮的约束s:

enter image description here

+0

这给了我最大的方式。我的假设是我可以有一个'| - [一个] - (> =空格) - [两个] - |'关系,按钮的大小可以自己调整,但事实并非如此。我添加了一些计算宽度的按钮,我到了那里。 –

+0

@ StephenH.Gerstacker你的假设是正确的。你的约束是现货。事实上,我只是在IB中创造了和你一样的约束条件。我必须做的唯一一件事就是调整其中一个按钮的压缩阻力优先级。所以你不必在这里计算按钮的宽度。我很快会在我的答案结尾处发布IB常量的屏幕截图。 – bilobatum

1

[targetView setNeedsUpdateConstraints]有礼貌地请求ui更新下次运行的约束,或者[targetView layoutIfNeeded]立即强制约束计算。在这里,你改变标题后,目标视图就是你的按钮。如何配置按钮上的内部UILabel以表现/调整大小会影响结果。

尝试如下所示。

#import "ADViewController.h" 

@interface ADViewController() 
@property (nonatomic) UIButton *button1; 
@property (nonatomic) UIButton *button2; 
@end 

@implementation ADViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    self.button1 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [self.button1 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.button1 setTitle:@"button1" forState:UIControlStateNormal]; 
    [self.button1.titleLabel setAdjustsFontSizeToFitWidth:YES]; 
    [self.button1 setTag:1]; 
    [self.button1 addTarget:self action:@selector(growTitle:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:self.button1]; 

    self.button2 = [UIButton buttonWithType:UIButtonTypeSystem]; 
    [self.button2 setTranslatesAutoresizingMaskIntoConstraints:NO]; 
    [self.button2 setTitle:@"button2" forState:UIControlStateNormal]; 
    [self.button2.titleLabel setAdjustsFontSizeToFitWidth:YES]; 
    [self.button2 setTag:2]; 
    [self.button2 addTarget:self action:@selector(growTitle:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:self.button2]; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 

    NSString *hString = @"H:|-[_button1(>=minButtonWidth)]-(>=minMiddle)-[_button2(>=minButtonWidth)]-|"; 
    NSString *vString1 = @"V:|-[_button1]-|"; 
    NSString *vString2 = @"V:|-[_button2]-|"; 

    NSDictionary *metrics = @{@"minMiddle": @40, 
           @"minButtonWidth": @100 }; 
    NSDictionary *views = NSDictionaryOfVariableBindings(_button1,_button2); 
    NSMutableArray *cons = [NSMutableArray arrayWithArray: 
          [NSLayoutConstraint constraintsWithVisualFormat:hString 
                    options:0 
                    metrics:metrics 
                     views:views]]; 
    [cons addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:vString1 
                     options:0 
                     metrics:metrics 
                     views:views]]; 
    [cons addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:vString2 
                     options:0 
                     metrics:metrics 
                     views:views]]; 
    [self.view addConstraints:cons]; 
} 

- (void)growTitle:(UIButton *)button 
{ 
    [button setTitle:[NSString stringWithFormat:@"%@-%ld", button.titleLabel.text, button.tag] 
      forState:UIControlStateNormal]; 
    [button setNeedsUpdateConstraints]; 
} 

@end