Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I disable autolayout for a specific subview at runtime?

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".

like image 911
terriblememory Avatar asked Jul 06 '12 19:07

terriblememory


People also ask

What is the use of Autolayout?

Auto Layout defines your user interface using a series of constraints. Constraints typically represent a relationship between two views. Auto Layout then calculates the size and location of each view based on these constraints. This produces layouts that dynamically respond to both internal and external changes.

How do I create a constraint in Autolayout?

There are three main options for setting up Auto Layout constraints in Interface Builder: You can control-drag between views, you can use the Pin and Align tools, and you can let Interface Builder set up the constraints for you and then edit or modify the results.

What is multiplier in Autolayout?

Multiplier is there for creating Proportional Constraint. Auto Layout calculates the first item's attribute to be the product of the second item's attribute and this multiplier . Any value other than 1 creates a proportional constraint.


2 Answers

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); 
like image 91
Muhammad Aamir Ali Avatar answered Sep 27 '22 22:09

Muhammad Aamir Ali


I had a similar issue where Autolayout was overriding some of my frame-setting at run time (I had a dynamic view that in some cases pushed a new view controller...pushing and then pressing Back would reset the initial view).

I got around this by putting my manipulation code in viewDidLayoutSubviews of my View Controller. This seems to get called after whatever constraint mojo gets called, but before viewDidAppear, so the user is none the wiser.

like image 36
jmstone617 Avatar answered Sep 28 '22 00:09

jmstone617