"iOS Development" multi-screen ruler automatic fitting AutoLayout (Pure code way)(2014-09-19 09:35:47) reprint
Tags: autolayout iOS development xcode UI |
Category: Xcode/ios/macos |
With regard to AutoLayout, it was first introduced from IOS6.
The main function is to use constraints, the relative layout of the view to adapt to different screen ruler transformation.
A lot of information on the web is introduced Xib and storyboard, how to use the AutoLayout, said that the pure code uses the AutoLayout to make the UI layout more and less. For me, a person accustomed to the code UI layout, write a memo:
what AutoLayout is. Using one of Apple's official definitions AutoLayout is a rule-based, descriptive layout system. Auto Layout is a constraint-based, descriptive Layout System. Keywords: based on constraints-and the position and size of the previously defined frame, the position of the AutoLayout is defined by a so-called relative position constraint, such as the center of the X coordinate Superview, and the y-coordinate is 10 pixels above the bottom of the screen. The definition of a constraint and the relationships of each view are described in a way that is close to the natural language or the visual language (which will be mentioned later) to describe the layout system-literally, the position of each element of the interface. In summary, AutoLayout provides developers with a different layout method than the traditional one for UI element locations. Before, whether in the IB drag-and-drop, or in the code to write, each uiview will have its own frame property, to define its position and size in the current view. Using AutoLayout, it becomes the use of constraints to define the position and size of the view. The biggest advantage of this is to solve the different resolution and screen size under the view of the problem, and also simplifies the rotation of the view location definition, originally in the bottom 10 pixel center view, Whether it's a rotating screen or a replacement device (an ipad or a iPhone5 or a future mini ipad), it's always centered 10 pixels above the bottom, and it doesn't change. Summarizing the use of constraints to describe the layout, the view frame is computed based on these constraints Describe the layout with constraints, and frames are calculated.
the difference between AutoLayout and autoresizing maskAutoresizing Mask is an old friend of ours ... If you've always written the UI in code, you must have written an enumeration like uiviewautoresizingflexiblewidth; If you used to use IB more, you must have noticed the size of each view Inspector has a red line autoresizing indicator and corresponding animation scaling diagram, which is autoresizing Mask. Prior to IOS6, the adaptation of the screen rotation and the automatic adaptation of the Iphone,ipad screen are basically done by the autoresizing mask. But with the growing demand for iOS apps, and the many screens and resolutions that are likely to emerge in the future, autoresizing mask seems a bit outdated and dull. AutoLayout can do all the work that the original autoresizing mask can do, while also being able to perform some tasks that could not have been done before, including: AutoLayout can specify the relative position of any two view, without the need for the like autoresizing Mask needs two view in the direct view hierarchy. AutoLayout you do not have to specify a constraint for an equality relationship, it can specify an inequality constraint (greater than or less than, and so on), whereas the autoresizing mask can only make a layout that is equal to the condition. The AutoLayout can specify the precedence of the constraint, and the calculation of the frame takes precedence over the conditions that satisfy the higher precedence. Summary autoresizing Mask is a subset of AutoLayout, and any work that can be done with autoresizing mask can be done with AutoLayout. AutoLayout also has some excellent features not available in Autoresizing mask to help us build the interface more easily.
AutoLayout Basic Use Method
Interface BuilderThis part of the online a lot of tutorials, are said this
manually adding constraints using the API
CreateA new class is added to the iOS6: Nslayoutconstraint, a constraint like this item1.attribute = multiplier? Item2.attribute + constant The corresponding code is
1 |
[Nslayoutconstraint Constraintwithitem:button |
2 |
Attribute:nslayoutattributebottom |
3 |
Relatedby:nslayoutrelationequa |
5 |
Attribute:nslayoutattributebottom |
The corresponding constraint is "the bottom of the button (y) = Superview bottom-10".
AddAfter you create the constraint, you need to add it to the action view. UIView (NSView, of course) has added a new instance method:-(void) AddConstraint: (Nslayoutconstraint *) constraint; Used to add a constraint to a view. The only thing to be aware of when adding is to add a target view to follow these rules: To add to their parent view the constraint relationship between two same-level view