【问题标题】:changing constraints at runtime in swift在运行时快速更改约束
【发布时间】:2015-09-27 04:44:27
【问题描述】:

我在 ios 的约束部分方面真的很弱。我浏览了许多参考链接,如下所示,但仍然无法解决我的问题。

Hide autolayout UIView : How to get existing NSLayoutConstraint to update this one

以防万一,我需要根据某些条件更改 UIView2 的 y 轴。 storyboard.png 显示布局。

如果条件在第 1 步的 UIView2 中失败,第 2 步和第 3 步应该是可见的,并且它的位置也应该改变,如 condition1.png 所示

如果条件通过 UIView2 与 step1,则 step 2 和 step3 应该不可见 如 condition1.png 所示

请提供如何实现上述场景的步骤。我试过这样做,但如果为 UIView1 设置约束,高度保持不变并且在运行时不会改变。

【问题讨论】:

    标签: swift uiview autolayout show-hide nslayoutconstraint


    【解决方案1】:

    只需制作 UIView 的高度出口,并使用常量属性来更改高度约束,例如...

    if conditionfails{
         heightconstOfView1.constant = 0
    }
    else{
        heightconstOfView2.constant = 0
    }
    

    【讨论】:

    • View1 的高度发生变化,但 View2 完全不动。
    【解决方案2】:

    我使用插座来设置情节提要上的约束,用于按钮之间的 div(使用旧方法设置动态布局)。因此,对于大屏幕(iPhone 6 及更高版本),我更新了固定高度的值(仅在纵向模式下查看应用程序)。对我有用的是:

    constHeightlogo.constant = 140 //old value = 72
    ///set any other updates here then force the view
    self.view.layoutIfNeeded()
    

    工作就像一个魅力。

    在此处查看相关内容:

    Update the constant property of a constraint programmatically in Swift?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-11
      • 2019-06-03
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多