3

I haven't been using Auto Layout because I need to support iOS 5. I'm now getting an NSInternalInconsistencyException when I push a controller:

Terminating app due to uncaught exception 'NSInternalInconsistencyException',
reason: 'Auto Layout still required after executing -layoutSubviews.
SGBExampleView's implementation of -layoutSubviews needs to call super.'

My implementation of layoutSubviews does call [super layoutSubviews], so that's unhelpful. It seems that something has turned on Auto Layout for my view; I'd like to turn it off again. However, everything I can find that says how to turn it off says to do so from IB. I don't use IB. How do I turn off Auto Layout for a view from code?

UPDATE:

This question is not a duplicate as the other pertains specifically to a UITableViewCell subclass, and setting translatesAutoresizingMaskIntoConstraints to either YES or NO has no effect.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
Simon
  • 25,468
  • 44
  • 152
  • 266
  • No, it isn't. The exception is the same, but that question has to do with how to get auto layout to work with table cells; My view isn't a table cell and I don't want to get auto layout to work, I want to turn it off. – Simon Jun 27 '13 at 09:27
  • agreed it's not a duplicate. The other question was specific to UITableViewCell – Max MacLeod Jun 27 '13 at 09:29
  • why are you using layoutSubviews if you are using auto layout? – Max MacLeod Jun 27 '13 at 09:30
  • I'd disagree. Docs state that layoutSubviews is when you need a level of control not offered by constraints (i.e. Auto layout). In any case, I'd say you are entering into treacherous waters. Could you give me an example project or docs that show how to use them together? – Max MacLeod Jun 27 '13 at 09:50
  • there's no problem mixing them in an app. I thought you were advocating using both layoutSubviews and Autolayout in the same class? – Max MacLeod Jun 27 '13 at 10:33
  • I don't think so. Could you show me some working code? – Max MacLeod Jun 27 '13 at 13:08
  • @Max MacLeod, feel free to create a new question if you want to get more information, for now I'm going to cleanup the comments as they don't help an author of this question a bit. – A-Live Jun 28 '13 at 09:59
  • sounds good. I don't think the question should have been closed though – Max MacLeod Jun 28 '13 at 10:10

2 Answers2

1

To turn off Auto Layout for a view, just don't have any constraints that relate to that view:

To provide additional compatibility, the entire constraint-based layout system is not active at all until you install a constraint on a view.

See Cocoa Auto Layout Guide. Yes, that relates to Cocoa and not iOS. However, the principle will be the same.

Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
  • Is there a way to turn it off if something else has added a constraint? Removing the existing ones doesn't seem to do the job. – Simon Jun 27 '13 at 13:16
  • why not override the addConstraint and addConstraints methods in your UIView subclass with methods that do nothing? – Max MacLeod Jun 27 '13 at 13:28
0

I've recently been added to a project that was initiated with auto layout, but I needed to dynamically adjust some frames in code. This is what I did to not be impaired by automatically generated run time constraints (courtesy of auto layout):

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