简体   繁体   中英

how to change components in UIViewController on runtime while using auto layout in iOS

I have a case in which I need to manipulate UIViewController 's component on runtime. The matter is I have adopted Auto Layout .

Now say I have a UIView called aView with Constraints plotted in a UIViewController .

Next, on the basis of my business logic, I need to remove aView and replace with other UIView called bView .

Here, when I remove aView , will all related constraints also removed OR it will remain as it is.

The question is if I simply remove aView and add bView will plot UIView same as before with old constraints or I need to re-implement all constraints?

Which is the best approach to change UIView s on runtime, considering using Auto Layout ?

When you remove aView and replace with bView the constraints will be gone and you will have to add them again. You can't take the constraints that belonged to aView and apply them to bView, you'll have to recreate them for bView.

It might be possible for you to skip autolayout for bView completely if it is just going to take the place of aView. If, in this case, the layout system has run and found a frame for aView you can just reuse that frame for bView.

It might be simpler to just add both aView and bView when the viewController loads and then switch one or the other to be hidden.

Why don't you just create a public method for the constraint programmatically, and just need to call the method for each time you create a new viewcontroller rather than copy and paste the method. Other solution is, creating a superclass of all the uiview that you want to have the same constraint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM