Ios horizontal and vertical screens have different effects. Therefore, if we allow switching between horizontal and vertical screens during development, we need to adjust the view layout. With Interface Builder development, we can quickly drag and drop the appropriate Interface layout, but the automatic screen switch layout cannot be well adapted. Yes, without any adjustments, you can see that the interface is not very beautiful.
Currently, I know three solutions for ios horizontal and vertical screen switching:
1. Use the Interface Builder adapter to automatically adapt and adjust the Interface.
2. When switching between the portrait and landscape screens, each control is re-laid.
3. Use Interface Builder to create two views. When the landscape is displayed, switch to the landscape view. When the landscape is displayed, switch to the landscape view.
In ios, the following function is called during screen switching:
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
- //zuo
- }
- if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
- //you
- }
- if (interfaceOrientation==UIInterfaceOrientationPortrait) {
- //shang
- }
- if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
- //xia
- }
- return YES;
- }
-
If yes is returned, the screen is switched. If no is returned, the view cannot be switched in the corresponding direction.
The following describes three methods. The first method is the simplest, but the effect is the worst. You only need to use Interface bulider to modify the corresponding attributes. The implementation result is as follows:
Implementation Method:
Select the control and press command + 3. The red line in the red box indicates that the distance cannot be automatically adapted. If the dotted line indicates that the distance can be automatically adapted. The final result is as follows.
Method 2:
The second method is to associate the corresponding control with the code:
Code:
- @interface ipad_demooViewController : UIViewController {
-
- IBOutlet UIButton *myButton1;
- IBOutlet UIButton *myButton2;
- IBOutlet UIButton *myButton3;
- IBOutlet UIButton *myButton4;
- IBOutlet UIButton *myButton5;
- IBOutlet UIButton *myButton6;
- }
- @property (nonatomic,retain) UIButton *myButton1;
- @property (nonatomic,retain) UIButton *myButton2;
- @property (nonatomic,retain) UIButton *myButton3;
- @property (nonatomic,retain) UIButton *myButton4;
- @property (nonatomic,retain) UIButton *myButton5;
- @property (nonatomic,retain) UIButton *myButton6;
-
- @end
-
Associated with IB:
Change the layout of each control:
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
- //zuo
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
- //you
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- if (interfaceOrientation==UIInterfaceOrientationPortrait) {
- //shang
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
- //xia
- self.myButton1.frame=CGRectMake(86, 208, 72, 37);
- self.myButton2.frame=CGRectMake(480, 208, 72, 37);
- self.myButton3.frame=CGRectMake(86, 308, 72, 37);
- self.myButton4.frame=CGRectMake(480, 308, 72, 37);
- self.myButton5.frame=CGRectMake(86, 408, 72, 37);
- self.myButton6.frame=CGRectMake(480, 408, 72, 37);
- }
- return YES;
- }
The third method is to create two views. The implementation process is as follows:
First, create two views:
- IBOutlet UIView *hView;
- IBOutlet UIView *vView;
-
Create the @ property method.
Then copy a view in IB.
The layout of a horizontal view and a vertical view. Connect the corresponding view with the corresponding method, and set a default view as view.
The following is the code implementation:
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
- if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
- //zuo
- self.view=self.hView;
- }
- if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
- //you
- self.view=self.hView;
- }
- if (interfaceOrientation==UIInterfaceOrientationPortrait) {
- //shang
- self.view=self.vView;
- }
- if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
- //xia
- self.view=self.vView;
- }
- return YES;
- }
-
The implementation result is as follows:
The above are the three horizontal and vertical screen solutions I know currently. We can see that the third method is relatively simple, but it is difficult to write and implement complicated logic. The second method is not intuitive, debugging is troublesome, but it works best.
Source code: http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demoo/
Ios6.0 horizontal/vertical screen switching Solution
This class is not key value coding-compliant for the key
The rotation method ios6 in ios5 does not fall, but it can still be used.
First, add
- -(NSUInteger) supportedInterfaceOrientations {
- Return UIInterfaceOrientationMaskAllButUpsideDown; // The value returned here depends on the direction you want to support. It must be the same as that in the plist file ).
- }
-
- -(BOOL) shouldAutorotate {
- Return YES; // supports screen Conversion
- }
These two functions.
Then, find the Supported interface orientations (iPad) option in the plist file and add any directions you want to support.
Then the problem is solved.
Maybe I still have a problem. I hope you can correct it. Thank you.
- -(NSUInteger) supportedInterfaceOrientations {
- Return UIInterfaceOrientationMaskAllButUpsideDown; // The value returned here depends on the direction you want to support. It must be the same as that in the plist file ).
- }
The settings here overwrite the values in plist.
Note: mainViewController must be set as the rootViewController of the window. problems may occur in the addSubView. In addition, all the above subviewcontrollers will be affected by the orientation supported by rootViewController.