IPhone and iPad often encounter horizontal and vertical screen switching or need to be adjusted automatically. If you cannot use storyboard or XIB to generate an interface, set the frame attribute value of the controller view and add the subview, you can use the autoresizingmask attribute of the view inheritance class (uiview). If you switch between the portrait and landscape screens or use methods such as uipopovercontroller, you can set the frame attribute only once, in the future, the frame attributes will be adaptive (if the frame is scaled down too much, the effect will be poor, depending on the situation ).
Principle: After autoresizingmask is set, when the page size changes, the system will Displayed,All related child views Make automatic adjustments. All controls in the attribute are automatically set to the attribute frame based on autoresizingmask. You can implement system callback in the corresponding-(void) setframe :( cgrect) rect, when the setframe method is called, the system automatically loads the default animation method.
The attributes of uiviewautoresizing are defined as follows:
{
Uiviewautoresizingnone = 0,
Uiviewautoresizingflexibleleftmargin = 1 <0,
Uiviewautoresizingflexiblewidth = 1 <1,
Uiviewautoresizingflexiblerightmargin = 1 <2,
Uiviewautoresizingflexibletopmargin = 1 <3,
Uiviewautoresizingflexibleheight = 1 <4,
Uiviewautoresizingflexiblebottommargin = 1 <5
};
Typedef nsuinteger uiviewautoresizing;
Uiviewautoresizingflexibleleftmargin view align to the right
Uiviewautoresizingflexiblewidth view adaptive width
Uiviewautoresizingflexiblerightmargin
Uiviewautoresizingflexibletopmargin view align bottom
Uiviewautoresizingflexibleheight view adaptive height
Uiviewautoresizingflexiblebottommargin
Note: The actual alignment directions of leftmargin, rightmargin, topmargin, and bottommargin are the opposite.
Example: Make the button always display in the upper right corner of viewcontroller:
-(Void) Viewdidload {[Super viewdidload]; uibutton* Right =[Uibutton buttonwithtype: uibuttontyperoundedrect]; right. Frame= Cgrectmake (self. View. Frame. Size. Width-300,0,300,300); Right. autoresizingmask= Uiviewautoresizingflexibleleftmargin | uiviewautoresizingflexiblebottommargin | uiviewautoresizingflexiblewidth |Uiviewautoresizingflexibleheight; [Right settitle:@"456"Forstate: uicontrolstatenormal]; [self. View addsubview: Right];}