This article turns from http://ruikq.github.io/ios/autolayout/uiscrollview/2015/01/27/iOS-autolayout%E6%80%BB%E7%BB%93.html AutoLayout, and Uiscrollview
Before learning iOS when not much contact autoLayout , since iPhone6 a 6+ out of the past has been to adapt to the previous app, so the use of a large number of adaptation, the autoLayout beginning is not used to, but the more used to feel good, contact with a lot of problems, here to summarize, Includes three parts: the priority of the restriction, the autoLayout Uiscrollview and the UITableView.
Priority level
In the beginning autoLayout of the use process, the priority is often ignored by me, so sometimes in some slightly complex layout often there are some very strange problems and warnings, especially the layout of some changes in size with the content of the control (UIButton, UILabel, Uiimageview), and these issues and warnings can be resolved by priority, following Uilabel as an example to summarize:
The above is the Uilabel limit, is the default priority, when the long Text button is clicked on the label with a long text, click the Short text button is short text.
First look at the Content Hugging Priority following section, I did not pay attention to this part when I started to use AutoLayout. The Content Hugging Priority meaning is to limit the content to greater priority, the following corresponding to landscape and portrait, Content Compression Resistance Priority is to limit the content to reduce the priority, the bottom intrinsic size is set content fixed size.
To better understand the meaning of these terms, the demo only focuses on landscape. Run demo Whether you click the long text or the short text button, the size of the label will not change. The following change some priority to make the label size change with the text size, first the label's trailing limit priority to 700, the other unchanged, and then run, found that the label can be larger with the text, but not smaller, This is because the right side of the label is less than 750 of the priority of the parent view, so the limit of the content becomes invalid, so when the label content becomes more restrictive, the label Trailing Constrain becomes smaller, but Content Hugging Priority the priority level is 251 less than 700, which is 700. When text is too young to prevent the label from getting larger, it now changes Content Hugging Priority to 800.
A warning appears, expecting a label width of 0, because the label text width is automatically calculated at the storyboard design stage to 0, so the label size is 0; You can remove the warning by setting intrinsic size to placeholder. This tells storyboard to set a temporary placeholder size, which is only valid at the storyboard design stage, does not affect the run time dimensions, and is now normal.
Uiscrollview
autoLayoutunder, Uiscrollview's contentsize is determined by the size of the content, and the dependency and normal child views depend on the parent view, which is the opposite. Therefore, the layout constraints of Uiscrollview's child view can not be determined by uiscrollview, so there are a lot of errors and warnings in the general case of constraints in Uiscrollview.
To deal with this situation, we need to determine the width and height of the Uiscrollview neutron view, but this is in conflict with the autoLayout wide and high variability, the current approach is to introduce an anchor view, and the width and height of the sub-view is determined by the anchor view.
Anchorviewforwidth is a view with a width and a parent view equal, a height of 0, a contentview of high fixation, width, and anchorviewforwidth equal, we must also set the Contentview top, trailing, Leading, button, which does not affect the size of the Contentview, which is equivalent to the narration of the Uiscrollview scrollable area, so a warning appears in the general view as repeating the set limit, but does not appear here.
The width dependency of the contentview in the vertical sliding Uiscrollview project can be set so that the height of the contentview above we are fixed, but if the height is determined with the runtime we can not set the fixed, in the iOS8 like Uilabel, UIButton, Uiimageview such as the size of the control with the content changes we do not need to set the height, the system will automatically calculate based on the content, if the content of the control is dynamically obtained, we can set the placeHolder placeholder size to pre-set;
After setting placeholder
For controls that cannot be evaluated, the runtime placeHolder controls are not visible, but the run-time control does not have visibility, so the run-time calculation modifies the constant by setting a height fix and then mapping the limit to a variable.
Precautions in Uiscrollview
1, in some cases uiscrollview can not scroll because autoLayout after use, after Viewdidload, the system will recalculate some of the value of the control will cause Uiscrollview contentsize to become (0,0), so you need viewDidLayoutSubViews to The Uiscrollview method is reset contentsize, but sometimes not on iOS7, iOS7 needs to be viewDidAppear:animated set contentsize in the method.
UITableView
Using AutoLayout in the UITableView cell, you can calculate the cell height based on the content itself, and in iOS8, as long as the tableview.rowheight is set UITableViewAutomaticDimension , the system automatically calculates the height based on the constraints set by the cell. , you need to use methods in IOS7 to systemLayoutSizeFittingSize: calculate the size of the cell based on constraints, while in iOS6 we need to manually calculate the cell's height.
UITableView use autoLayout than Uiscrollview to be simple, the only thing that I get into trouble is tableHeaderView that in the Xib file after the addition tableHeaderView is not able to change his position, nor the use of autoLayout increased constraints, which can not dynamically change tableHeaderView the height of After searching for StackOverflow, we find that it is not necessary to set the AutoLayout to Tableheaderview, and change the height of the Tableheaderview directly to alter the frame.
CGRectHeaderframe=Self.Listview.Tableheaderview.Frameheaderframe. Size. Height = 47; Self. Listview. Tableheaderview. Frame = headerframe; [self. ListView settableheaderview:headerview];self. Listview. Contentoffset = cgpointzero
For an externally customized view tableHeaderView , you cannot set the frame size in a custom view, you must also reset the frame as above, otherwise there will be a tableHeaderView strange phenomenon of occlusion cell, tableheaderview stretching, and display.
Summarize
AutoLayout is IOS6 on the proposed things, at first because of poor experience, the operation of the people are very few, but later in the development of it is essential, so I think of swift, although now the version is not mature, but must slowly replace the Object_c, Because Apple doesn't make a chicken. Contact AutoLayout has been for several months, is also suitable for a lot of ui,autolayout is a more use more comfortable things, if there is a strange problem that a point has not mastered, need to learn carefully. Finally, the demo code is attached.
IOS AutoLayout Summary