4

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.

Community
  • 1
  • 1
George
  • 3,384
  • 5
  • 40
  • 64

2 Answers2

9

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]
Thomas Keuleers
  • 6,075
  • 2
  • 32
  • 35
  • 1
    To @ThomasKeuleers, But I didn't add any constraints into any view. I heard about that `translatesAutoresizingMaskIntoConstraints` is default to `YES`, maybe some autoresizing mask change into constraint. But this change happens when Auto Layout is turned on by at least constraint.... – George Nov 05 '13 at 12:24
  • To @ThomasKeuleers, How I can force turn off Auto Layout? Can I do all of these: (1) Remove all constraints of all views; but I don't know when to do it, in `viewDidLoad`? (2) Set`translatesAutoresizingMaskIntoConstraints` to `NO` for all views; don't know when to set it either. (3) Set `autoresizesSubviews` to `NO` for all views; don't know when to set it either. – George Nov 05 '13 at 12:27
  • @congliu Ok, my post only describes to remove the constraints if there are any. I found another post where someone claims to have found a workaround for your problem http://stackoverflow.com/questions/11368440/can-i-disable-autolayout-for-a-specific-subview-at-runtime/15242064#15242064 – Thomas Keuleers Nov 05 '13 at 12:40
  • To @ThomasKeuleers, Finally I find a work-around for my wrestling with auto layout: http://stackoverflow.com/questions/19785071/scroll-bar-of-ios-web-view-change-into-half-of-it-should-be/19793515#19793515 – George Nov 05 '13 at 16:12
  • @congliu I'm glad you did :) Thanks for the info on how you solved it! – Thomas Keuleers Nov 05 '13 at 17:28
7

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.

Yup.
  • 1,883
  • 21
  • 18