【问题标题】:Two Buttons Fighting with AutoLayout两个按钮与 AutoLayout 战斗
【发布时间】:2014-03-18 19:03:56
【问题描述】:

我的视图应该是这样的

 ----------------------------------
| [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] |
 ----------------------------------

当我更新按钮文本时,我还需要做什么吗?

【问题讨论】:

    标签: ios cocoa-touch autolayout


    【解决方案1】:

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

    对于右键(标题可能没有被压缩),增加它的抗压缩优先级。默认抗压优先级为 750。将其增加到 751 就足够了。

    [button2 setContentCompressionResistancePriority:751 forAxis:UILayoutConstraintAxisHorizontal];
    

    如果你在 IB 中这样做:

    左键(标题可以压缩):

    右键(标题不可压缩):

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

    【讨论】:

    • 这让我很顺利。我假设我可以有一个|-[one]-(>=space)-[two]-| 关系,其中按钮会自行调整大小以适应,但似乎情况并非如此。我为按钮添加了一些计算宽度,然后就到了。
    • @StephenH.Gerstacker 您的假设是正确的。你的限制是正确的。事实上,我只在 IB 中创建了与您相同的约束。我唯一需要做的就是调整其中一个按钮的抗压优先级。所以你不应该在这里计算按钮的宽度。我很快会在上面的答案末尾发布我在 IB 中的常量的屏幕截图。
    【解决方案2】:

    [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
    

    【讨论】:

      猜你喜欢
      • 2015-09-27
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多