Automatic resizing behavior
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. |
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.
Uiviewautoresizingnone is not automatically adjusted.
Uiviewautoresizingflexibleleftmargin automatically adjusts the distance to the left of the superview, ensuring that the distance to the right of the Superview is constant.
Uiviewautoresizingflexiblerightmargin automatically adjusts to the right distance from the Superview to ensure that the distance to the left of the Superview is constant.
The Uiviewautoresizingflexibletopmargin automatically adjusts the distance from the top of the Superview to ensure that the distance from the bottom of the Superview is constant.
Uiviewautoresizingflexiblebottommargin automatically adjusts the distance from the bottom of the Superview, that is, the distance from the top of the Superview is constant.
Uiviewautoresizingflexiblewidth automatically adjusts its width to ensure that the distance to the left and right of the Superview is constant.
Uiviewautoresizingflexibleheight automatically adjusts its height to ensure that the distance from the top and bottom of the Superview is constant.
Uiviewautoresizingflexibleleftmargin | Uiviewautoresizingflexiblerightmargin automatically adjusts the distance to the left of the superview to ensure that the distance to the left and to the right is the same as the distance from the left and right. For example, the original distance is 20, 30, the adjusted distance should be 68,102, that is, 68/20=102/30.
Other combinations are similar.
iOS UIView Autoresizingsubview Properties