In the development of the ipad, the view of the vertical and horizontal screen is often different, such as the sidebar dock, the screen is used for wider screens, can display enough content, each button can display the title, while the dock should be relatively narrow when the screen, only show the icon is not realistic button title.
The important knowledge of the ipad is that the wide height of different types of devices is fixed to 768x1024 in a graphical coordinate system in points, so this value is often used to determine the screen.
768, 1024, the screen judge is very common, you can define a common constant file to put these content.
"Definitions of constants and macros"
① in. m files, define variables, plus const, and note that pointers (such as nsstring*) use constants instead of constant pointers.
The width of the screen when the vertical screen is const cgfloat HMSCREENPW = 768;//the width of the screen horizontal const cgfloat HMSCREENLW = 1024;
For the definition of pointer volume:
NSString *const title = @ "title";
② in the. h file, use the extern declaration above variables and use the macro to determine the screen.
The width of the screen when the vertical screen is extern const CGFloat hmscreenpw;//the width of the screen horizontal extern const cgfloat HMSCREENLW; #define LANSCAPE ([UIScreen MAINSCR Een].bounds.size.width = = HMSCREENLW) #define PORTRAIT ([UIScreen mainscreen].bounds.size.width = HMSCREENPW)
"The key approach to adaptation"
① the way the controller is called when the screen is rotated:
-(void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (NSTimeInterval ) duration;
Judging the screen by function:
Uiinterfaceorientationislandscape (tointerfaceorientation)
Common usage:
Note that the screen rotation is not finished when this method is called, and the wide height you get here will be wrong (original).
-(void) Willrotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation Duration: (NSTimeInterval duration{ //Call this function has not completed the rotation, the current height is the old if (Uiinterfaceorientationislandscape (tointerfaceorientation)) {//Horizontal screen } else {//vertical screen } }
In IOS8, the above method has expired and is updated to the following method:
Note that the method above is not valid, so the above method should be used in order to fit the method.
Implement the following method above the expiration method will not be called, in order to adapt the old method should be used. -(void) Viewwilltransitiontosize: (cgsize) size withtransitioncoordinator: (id< uiviewcontrollertransitioncoordinator>) coordinator{ NSLog (@ "%@", nsstringfromcgsize (size)); }
②uiview the Layoutsubviews method is called after the dimension has been modified, you can override this method toCall the parent class method first, then implement their own layout logic, so that the dock and so on the screen transformation.
-(void) layoutsubviews{ [Super layoutsubviews]; Your own layout logic}
"Segmentation of complex views"
For a complex view, it is often necessary to slice it multiple times, and then handle it separately, for example, the following view's sharding:
First, the entire controller view is cut into the contentview and the dock, and the dock is subdivided inside, the dock should be narrower on the vertical screen, all the buttons below are vertically arranged, and the caption is not displayed, as shown in:
Once the segmentation is complete, the processing adaptation becomes simple. First, the screen rotation will call the controller's method, the controller directly modifies the dock size, which calls the dock's Layoutsubviews method, the dock in this method Headview, Tabbar, toobar the size of the changes, They also call their respective layoutsubviews methods, because they only need to handle dimensional changes in layoutsubviews.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(108) Screen fitting for ipad development