Auto Layout-Usage Summary

Source: Internet
Author: User

self.view.translatesAutoresizingMaskIntoConstraints = NO;  Self.view represents all the view you want to add a constraint to

2. In a view that uses AutoLayout layout, the code avoids the code that sets its frame-related properties (such as center), but can get its frame;

3, when the code for Xib or SB in the view to increase the constraints, try to avoid viewdidload in the execution, preferably in updateviewconstraints[uiviewcontroller] or updateconstraints[ UIView], remember to call [Super updateviewconstraints] or [super updateconstraints];

-(void) updateviewconstraints{    //To add a constraint to your view here, make sure the view's Translatesautoresizingmaskintoconstraints property is set to No    [Super updateviewconstraints];}

If you do write in Viewdidload, you may encounter this kind of crash error

4. When you set a uiscrollview inside a child control relative to the Uiscrollview, the constraint affects only the contentsize calculation of Uiscrollview and does not affect the size of the Uiscrollview.

Add container to Uiscrollview by the view structure in, and then add all the child controls to the container

① set scroll view's constraint relative to its parent control-guarantees the correct location and size of the ScrollView

② sets the constraint of the container relative to the scroll view-typically scrollview of container distance trailing/leading/top/bottom are 0. This allows the scrollview to be calculated by the size of the constraint and container contentsize

③ adds child controls to container and constrains the width and height of the container to accommodate child controls

If you want to set container to a fixed width and equal to the width of the controller view, in this case you can use code to set the container with a leading/trailing of 0 relative to Self.view.

1-(void) updateViewConstraints2 {3     nslayoutconstraint *sctleading = [Nslayoutconstraint constraintwithitem:_ Container attribute:nslayoutattributeleading relatedby:nslayoutrelationequal ToItem:self.view attribute: Nslayoutattributeleft multiplier:1.0 constant:0];4     nslayoutconstraint *csttrailing = [NSLayoutConstraint Constraintwithitem:_container attribute:nslayoutattributetrailing relatedby:nslayoutrelationequal ToItem: Self.view attribute:nslayoutattributeright multiplier:1.0 constant:0];5     6     [Self.view addConstraints:@[ Csttrailing, sctleading]];7     8     [Super Updateviewconstraints];9}

5. A simple method for setting a fixed number of view spacing distributions in AutoLayout

For example, add a view in the horizontal direction [space means occupy the gap position with transparent view]

Space1-view1-space2-view2-space3-view3-space4

Set space leading = super.leading + 0/space4.trailing = super.trailing + 0 4 space width, height can be set freely [but cannot be set] All view widths are the same.

This is a much simpler way to drag 3 view directly than normal, and then add constraints in order to be quick and easy, and to have little impact on performance in a small number of cases.

6, if you need to dynamically add or remove views in the controller, add constraints to the newly added view in the controller, implement in Updateviewconstrains, and then call [Super Updateviewconstrains]; Dynamically add or remove views in view, implement in Updateconstrains, and invoke [Super Updateconstrains]

7. The controller performs the following procedures when its view needs to be re-layout:

The view of the ① controller adjusts to the new dimensions-the controller adjusts the position size of the view based on the status of the current status bar, navigation bar, and other factors

② If AutoLayout is not used, all child views are adjusted according to Autoresizeing mask

③ Call Viewwilllayoutsubviews

④ calls the layoutsubviews of the controller view, and if it is using AutoLayout, the implementation of the method called Updateviewconstrains----will invoke all child views of updateconstraints- After the constraint is updated, all views update the location based on the new calculated layout

⑤ calling the controller's Viewdidlayoutsubviews

If you have questions about the above, please leave a message to discuss. Reprint please specify Source: Blog Park @bbqe http://www.cnblogs.com/liufeng24/

Auto Layout-Usage Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.