iOS UIView Auto Size adjustment

Source: Internet
Author: User


When you change the bounding rectangle of a view, the position and dimensions of the inset child view often need to be changed to fit the new dimensions of the original view. If the view's Autoresizessubviews property declaration is set to Yes, its child view is automatically resized based on the value of the Autoresizingmask property. Simply configuring the automatic sizing mask for the view often allows the application to get the appropriate behavior, otherwise the application must provide its own implementation by overloading the Layoutsubviews method.

The way to set the automatic resizing behavior of a view is to link the desired automatic sizing constants with the bitwise OR operator and assign the results to the Autoresizingmask property of the view. Table 2-1 lists the automatic sizing constants and describes how these constants affect the size and position of a given view. For example, if you want to keep the relative position of the lower-left corner of a view and its parent view intact, you can add Uiviewautoresizingflexiblerightmargin and Uiviewautoresizingflexibletopmargin constants and assigns the result to the Autoresizingmask property. When multiple parts of the same axis are set to variable, the margin for sizing is evenly distributed across the sections.



Uiviewautoresizingnone

If this constant is set, the view will not be automatically resized.

Uiviewautoresizingflexibleheight

If this constant is set, the height of the view will change proportionally with the height of the parent view. Otherwise, the view's height will remain unchanged.

Uiviewautoresizingflexiblewidth

If this constant is set, the width of the view will change proportionally with the width of the parent view. Otherwise, the width of the view remains the same.

Uiviewautoresizingflexibleleftmargin

If this constant is set, the left edge of the view adjusts proportionally as the width of the parent view changes. Otherwise, the relative position of the view and its parent view's left edge remains the same.

Uiviewautoresizingflexiblerightmargin

If this constant is set, the right edge of the view adjusts proportionally as the width of the parent view changes. Otherwise, the relative position of the right edge of the view and its parent view remains the same.

Uiviewautoresizingflexiblebottommargin

If this constant is set, the bottom edge of the view adjusts proportionally as the height of the parent view changes. Otherwise, the relative position of the bottom boundary of the view and its parent view remains unchanged.

Uiviewautoresizingflexibletopmargin

If this constant is set, the top bounds of the view will adjust proportionally as the parent view height changes. Otherwise, the relative position of the top boundary of the view and its parent view remains unchanged.

Figure 2-8 provides a graphical representation of the positions of these constant values. If one of these constants is omitted, the layout of the view in the corresponding direction is fixed, and if a constant is included in the mask, the layout of the view in that direction is flexible.

Figure 2-8 automatic resizing mask constants for views

If you configure the view through Interface Builder, you can use the autosizing control of the size viewer to set the automatic resizing behavior for each view. The flexible width and height constants in the interface builder have the same behavior as the springs in the same position, but the behavior of the blank constants is exactly the opposite. In other words, if you want to apply the flexible right blank automatic resizing behavior to a view of interface builder, you must control the autosizing of the corresponding direction space to NULL instead of placing a pillar. Fortunately, Interface Builder animates the effect of your changes on the automatic resizing behavior of the view.

If the Autoresizessubviews property of the view is set to No, all automatic resizing behavior of the view's immediate child view is ignored. Similarly, if the automatic sizing mask for a child view is set to Uiviewautoresizingnone, the dimensions of the child view will not be adjusted, and the dimensions of its immediate child views will not be adjusted.

Note: in order for the automatic sizing behavior to be correct, the transform property of the view must be set to the identity transform, and the dimension auto-adjust behavior under other transformations is undefined.

The automatic sizing behavior can be appropriate for some layout requirements, but if you want more control over the layout of the view, you can overload the Layoutsubviews method in the appropriate view class. For more information about view layout management, see the "Changes in response layouts" section.

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.