Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically turn off iOS 7 auto layout

I already know that we can turn it off by unchecking File Inspector > Interface Builder Document > Use Auto Layout, when we use interface builder.

But in my case, I don't use interface builder nor storyboard, only programmatically created views. Then how can I turn off Auto Layout for iOS 7 using Xcode 5?

How do I turn off Auto Layout for a view from code? tells us that auto layout is turned off if we don't create any constraints for any view. I'm not sure how to create constraints, so I don't if I did some mistakes that lead to auto layout turning on. I want auto layout be off.

like image 277
George Avatar asked Dec 04 '22 08:12

George


2 Answers

When you programmatically create a UIView, there aren't any layout constraints defined. You have to add them manually. If you do have some layoutconstraints configured for a certain view, they can be removed like so:

[view removeConstraints:view.constraints]
like image 149
Thomas Keuleers Avatar answered Mar 11 '23 14:03

Thomas Keuleers


So this answer was deleted by Andrew Barber, but its a solid work around and it's helpful so i'm posting it again:

1) Do NOT have views or components laid out in interface builder.

2) Add your views purely programmatically starting with alloc/init and setting their frames appropriately.

3) Done.

Hope that helps!

ps. you can also experiment with stripping constraints from views with:

[view removeConstraints:view.constraints] but i've had more luck with the pure code approach.

like image 44
Yup. Avatar answered Mar 11 '23 16:03

Yup.