Can I disable autolayout for a specific subview at runtime?

terriblememory picture terriblememory · Jul 6, 2012 · Viewed 84k times · Source

I have a view that needs to have its frame manipulated programmatically - it's a kind of document view that wraps to its content which is then scrolled and zoomed around a superview by manipulating the frame origin. Autolayout fights with this at runtime.

Disabling autolayout completely seems a bit harsh because it could reasonably be used to handle layout for the other views. It seems like what I might want is some kind of "null constraint".

Answer

Muhammad Aamir Ali picture Muhammad Aamir Ali · Mar 6, 2013

I had the same problem. But I have resolved it.
Yes, you can disable auto layout at runtime for a specific UIView, instead of disabling it for the whole xib or storyboard which is set by default in Xcode 4.3 and later.

Set translatesAutoresizingMaskIntoConstraints to YES, before you set the frame of your subview:

self.exampleView.translatesAutoresizingMaskIntoConstraints = YES;
self.exampleView.frame = CGRectMake(20, 20, 50, 50);