changing constraints at runtime in swift

冷暖自知 提交于 2019-12-09 03:46:37

问题


I'm really very weak in constraints part of ios. I have gone through many reference link as shown below but still not able solve my issue.

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

In case , i need to change the y-axis of UIView2 based on some condition. The storyboard.png shows the layout.

If condition fails UIView2 with step1, step 2 and step3 should be visible and it's position should also change as shown in condition1.png

If condition pass UIView2 with step1, step 2 and step3 should not be visible as shown in condition1.png

Please provide me steps for how to achieve the above scenario. I tried it doing but if set the constraint for UIView1 the height remains same and does not change at runtime.


回答1:


just make the outlet of height of both UIView and use constant property to change the height constraints like...

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



回答2:


I use outlets for constraints set on the storyboard, for divs between buttons (using old ways to set dynamic layouts). So, for large screens (iPhone 6 and above), I update the values of fixed heights (app only viewed in portrait mode). What worked for me is:

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

Worked like a charm.

See related here:

Update the constant property of a constraint programmatically in Swift?



来源:https://stackoverflow.com/questions/32804655/changing-constraints-at-runtime-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!