Problem
Some third party library is used. Some views disappear after their translatesAutoresizingMaskIntoConstraints are set to NO.
Don't have other autoresizingMask setting for my views in my own code; in the library, the autoresizingMask parts of code have been removed too. Instead, explicit bounds/center/frame are set for those views. There're no nib files, views are all programmatically created.
I know some other people solve similar problem by giving a thorough autolayout constraints set, but in my case, I mean to turn off autolayout and do it manually. No idea when autolayout is turned on.
Some people say that "by default, as your app launches, autolayout is switched off, and the system behaves as in iOS 5 and before. But if, at any time while your app runs, the system sees an autolayout constraint (generated in code or by the loading of a nib that has “Use autolayout” checked), the autolayout system is switched on, and from then on you’re running under autolayout." (Programming iOS 6 by Matt Neuberg, pages 383-384), but in my project I don't think there's any autolayout constraints left.
The code is bulky, but will upload some skeleton if necessary.
Thank you for tips!
If you don't want to use Auto Layout you have to set translatesAutoresizingMaskIntoConstraints to YES. Here's a reference:
This works through the property
translatesAutoresizingMaskIntoConstraints. When this property is YES,
which it is by default, the autoresizing mask of a view is translated
into constraints. For example, if a view is configured as in Figure
6-1 and translatesAutoresizingMaskIntoConstraints is YES, then the
constraints |-20-[button]-20-| and V:|-20-[button(20)] are added to
the view’s superview. The net effect is that unaware views behave as
they did in versions of OS X prior to 10.7.
For views that are aware of Auto Layout, in most circumstances you
will want translatesAutoresizingMaskIntoConstraints to be NO. This is
because the constraints generated by translating the autoresizing mask
are already sufficient to completely specify the frame of a view given
its superview’s frame, which is generally too much. For example, this
will prevent a button from automatically assuming its optimal width
when its title is changed.
Related
There is autosizing option available until you give any constraints to UI Component.
So can I use both for my ViewController?
You could, but you shouldn't
You can use constraints on some views, and autosizing on others, but be careful not to mix them on the same view, as that will cause issues (the autosizing information will be lost).
iOS takes care of autosizing views by creating constraints that convey the information of the autosizing to the constraints engine. This behaviour can be enabled or disabled by the aptly named translatesAutoresizingMaskIntoConstraints property.
You could try to add constraints to a view and still have it autoresize with the old behaviour by setting this value to true, but I suggest you use constraints for every view, as it can do everything the autosizing can, and much more.
Yes, you can (use a mixture of constraints and autoresizing on subviews within a single view) within Xcode 8. See:
02:38 https://developer.apple.com/videos/play/wwdc2016/236/
With Xcode 8, the translatesAutoresizingMaskIntoConstraints property for each view is automatically maintained by Interface Builder:
By default, when a child view is first added, the value of the property is true.
When the first constraint is added to the child view, the value is automatically set to false and the Size Inspector pane changes to the Constraints view.
When the last constraint is removed from the child view, the value of the property is automatically reverted to true, and the Size inspector reverts to the Autoresizing view.
If you create a view programatically, Xcode will generate constraints to satisfy autoresizing itself (translatesAutoresizingMaskIntoConstraints is set to true). However if you create a view in IB, Apple says:
If you add views in Interface Builder, the
system automatically sets this property to false.
To reflect that, any view added into IB before Xcode 8 has that autoresizing option hidded (or at least I did not see it for a while there).
But here is the thing:
Since Xcode 8, the option is visible (translatesAutoresizingMaskIntoConstraints is set to true) until you add any constraint, so the Apple's text above is not actually correct I guess.
So in a single view you cannot satisfy/use both. view(autoresizing and constraints)
In subviews you can use different for each one, but it will be a mess and I cannot imagine reasonable usage. view->subview(constraints) and subview(autoresizing)
That also means that you can use e.g. view(autoresizing)->subview(constraints)
I have a project where some of my views use constraints and some of them don't really need to because they show up nicely on other device sizes as well. My problem is that when I deleted all my constraints in a view controller's subviews, The subviews get heavily misplaced even though I didn't get any warnings about missing constraints.
Also, when I try to "reset all my subviews to suggested constraints", this adds no constraints whatsoever, and I'm stuck with misplaced views.
What could be the cause of this? Constraints are very tricky to work with.
Because,AutoLayout in XCode is default enabled.
Even you do not create any your constraints, XCode will auto create some constraints at build time so that iOS will know how to render the views
If you want to disable autoLayout,just uncheck the Use auto layout as image shown below
I have been searching the proper difference between Auto Layout and Auto Resizing, but didn't able to find the exact answer. Where I can use "Auto Layout" and where "Auto Resizing" in app? Any help would be a part of thanks.
As Matt Neuburg states in his book:
Autoresizing is a matter of conceptually assigning a subview “springs
and struts.” A spring can stretch; a strut can’t. Springs and struts
can be assigned internally or externally. Thus you can specify (using
internal springs and struts) whether and how the view can be resized,
and (using external springs and struts) whether and how the view can
be repositioned.
And
Autolayout, depends on the constraints of views. A constraint (an
instance of NSLayoutConstraint) is much more sophisticated than the
"autoresizingMask" it’s a full-fledged object with numeric values, and
can describe a relationship between any two views (not just a subview
and its superview).
I recommend watching the WWDC 2012 session https://developer.apple.com/videos/wwdc/2012/?id=202
Auto Layout is a new way to define dynamic GUIs. Before, we had autoresizing masks, that described how a subview will resize or move when its superview is resized. With Auto Layout you can do the same and also a lot more complicated GUIs quite easily.
Autoresizing is one of the most useful property for layouting the views in their hierarchies.
go through this link.
http://www.techpaa.com/2012/05/understanding-uiview-autoresizing.html
AutoResizing : Autoresizing means that how the content of a view will fit to the view. It probably depends on the self content of the view.
AutoLayout: AutoLayout means how the external constraints like the position of the view , the size of view supported by the other neighboring components.
AutoLayout triggers the AutoResizing Task and for autoresizing activity , auto-layout forcefully or normally change or break the default constraints of views which are related to the corresponding view if needed.
Use autolayout in the superview with constraints [this helps when there are changes in the space in the View due to different devices(such as 3.5 or 4 inches retina)] and use autoresize for resizing the GUI objects in a view [this helps when there are changes in the space in the View during the orientation of the device]
I want to ask if it is better to have autolayout turned off when I want my application's views to adjust themselves properly to match the device's screen orientation when the user rotates the iOS device from portrait to landscape and vice-versa.
It's because when I have autolayout off, I can adjust my view's autosizing and masks manually via the size inspector. The views seem to adjust fine to match the screen orientation.
I have already tried this with autolayout on and when I rotate the device, the views don't put themselves in place properly for some reason. This only happens for some apps though(I guess it depends on the original placement of the views in portrait mode)
Am I just missing something like a feature or setting for the autolayout that fixes this automatically? Or is it better to go with autolayout turned off?
Thanks
My recommendation is to use auto layout. It sounds from your question like you have not done much work with autolayout or the constraints that are generated for you. Yes, the generated constraints depend on the original placement of the view in portrait mode, but you will need to review and possibly change the constraints to make them work as you expect in landscape.
One of the key ideas driving auto layout is to specify how your views relate to each other so that iOS knows how to draw them in various circumstances, eg, rotating.
You mentioned that the views don't put themselves in place properly when rotated. It sounds to me like the generated constraints are not setting the spacing from a leading or to edge to the super view for something important.
WWDC videos cover autolayout concepts but beware that how and when you specify constraints has changed in XCode 5, so be sure to review the XCode videos from WWDC '13 for those changes.
In one of the WWDC 2012 videos, Auto Layout By Example, they demo an OS X app using Autolayout, and at about 7 or 8 minutes in, he shows how for a single view, you can uncheck a box in the Attributes Inspector, and the box is called something like "Translates Autoresizing Mask Into Constraints". Now, I'm well aware of the code equivalent of this box, the translatesAutoresizingMaskIntoConstraints boolean, but I can't seem to find this checkbox anywhere in either iOS or OS X projects. My project uses Autolayout. I really would like this checkbox, because one of things I'm struggling with in learning Autolayout (and converting a springs/struts app to AL) is the million constraints Xcode has generated for each view, and how to clean them up and sensibly override some/all in code. What I'd like in order to do this conversion one view at a time is to turn off those auto-generated constraints.
Why can't I see this checkbox? I'm using Xcode 4.6.
That checkbox is available in Interface Builder (IB), but only if you are developing Cocoa projects targeted for OS X. For iOS, it's not available at present. You can only set it programmatically.
Auto Layout on iOS from my understanding - and others feel free to pitch in here - is not a full implementation of what is available on OS X.
To be honest, given what you say afterwards, this checkbox is probably something you don't need to worry about. I think it is important in upgrading OS X projects to Auto Layout, but generally for iOS it's unlikely you'll be mixing one and the other. I.e., you either checkbox your Xib in the File Inspector to "Use Autolayout" or you don't.
That said, there is one use case where you may need to mess with that flag. That's if you want to create a standalone Xib file for a view, and then load that programmatically using loadNibNamed. When doing that, by default old style Auto Resizing constraints are converted into new style Auto Layout constraints. Typically I want to add my own so I set that flag to zap 'em.
myView.translatesAutoresizingMaskIntoConstraints = NO
Anyway that's another story.
Here's the link for more info, although you've no doubt had a look at it already:
Adopting Auto Layout
One thing I'd say is that if you're struggling with Auto Layout in the beginning - and you wouldn't be human if you weren't, we all have been - then I'd stick with Interface Builder and think about the golden rules. The most important one for me is that it hates ambiguity. It's like a vacuum in nature. Before you can delete the constraint that you don't want, you have to add the one that you do want then zap the old one.
The other mistake that I made was mixing Auto Layout and frames. So I'd do some code that checked the frame width then apply that to the constraints. Bad mistake. That really ends in tears. When you get into Auto Layout it's essential to really forget about doing anything with CGRect, frame, etc.
Stick with it though. Start with some simple views in IB and experiment. There is method to the madness, really.
One more link also worth looking at is:
10 Things You Need To Know About Cocoa Autolayout