【问题标题】:Adding UI elements between elements using autolayout使用自动布局在元素之间添加 UI 元素
【发布时间】:2014-06-11 11:08:33
【问题描述】:

我有两个使用 autolayout 的 UIView。所有约束都在 xib 中定义。我们如何使用自动布局在视图 1 和视图 2 之间添加另一个视图而不会产生任何冲突。

【问题讨论】:

    标签: ios objective-c xcode uiview autolayout


    【解决方案1】:

    您需要将视图 1 和视图 2 之间的约束降级。要么将其设为低优先级,要么将 equals 常量更改为小于或等于。或者,您可以保存对该约束的出口引用,并在添加新视图时将其删除。

        // Add the view as subview
        [self.view addSubview:view3];
        [view3 setTranslatesAutoresizingMasksIntoConstraints:NO]
    
        // Make the constraints
        NSDictionary *views = NSDictionaryOfVariableBindings(view1, view2, view3);
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[view1]-10-[view3]-10-[view2]" options:0 metrics:nil views:views]];
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[view3]-|" options:0 metrics:nil views:views]];
    
        // Tell view to update
        [self.view layoutIfNeeded];
    

    如果您要为视图设置动画,则除了只将最后一行放在动画块中之外,您将希望执行所有操作。您可能还想为视图设置一个起始帧,否则它将默认从左上角开始动画。

    【讨论】:

      猜你喜欢
      • 2017-12-05
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多