IOS horizontal and vertical screen Solution

Source: Internet
Author: User

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:

 
 
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  2.         if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
  3.             //zuo 
  4.         } 
  5.         if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
  6.             //you 
  7.         } 
  8.         if (interfaceOrientation==UIInterfaceOrientationPortrait) { 
  9.             //shang 
  10.         } 
  11.         if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
  12.             //xia 
  13.         } 
  14.         return YES; 
  15.     } 
  16.   

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:

 
 
  1. @interface ipad_demooViewController : UIViewController { 
  2.  
  3.        IBOutlet UIButton *myButton1; 
  4.         IBOutlet UIButton *myButton2; 
  5.         IBOutlet UIButton *myButton3; 
  6.         IBOutlet UIButton *myButton4; 
  7.         IBOutlet UIButton *myButton5; 
  8.         IBOutlet UIButton *myButton6; 
  9.     } 
  10.     @property (nonatomic,retain) UIButton *myButton1; 
  11.     @property (nonatomic,retain) UIButton *myButton2; 
  12.     @property (nonatomic,retain) UIButton *myButton3; 
  13.     @property (nonatomic,retain) UIButton *myButton4; 
  14.     @property (nonatomic,retain) UIButton *myButton5; 
  15.     @property (nonatomic,retain) UIButton *myButton6; 
  16.  
  17.     @end 
  18.   

Associated with IB:

Change the layout of each control:

 
 
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  2.        if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
  3.            //zuo 
  4.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  5.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  6.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  7.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  8.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  9.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  10.        } 
  11.        if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
  12.            //you 
  13.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  14.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  15.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  16.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  17.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  18.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  19.        } 
  20.        if (interfaceOrientation==UIInterfaceOrientationPortrait) { 
  21.            //shang 
  22.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  23.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  24.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  25.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  26.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  27.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  28.        } 
  29.        if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
  30.            //xia 
  31.            self.myButton1.frame=CGRectMake(86, 208, 72, 37); 
  32.            self.myButton2.frame=CGRectMake(480, 208, 72, 37); 
  33.            self.myButton3.frame=CGRectMake(86, 308, 72, 37); 
  34.            self.myButton4.frame=CGRectMake(480, 308, 72, 37); 
  35.            self.myButton5.frame=CGRectMake(86, 408, 72, 37); 
  36.            self.myButton6.frame=CGRectMake(480, 408, 72, 37); 
  37.        } 
  38.        return YES; 
  39.    } 

The third method is to create two views. The implementation process is as follows:

First, create two views:

 
 
  1. IBOutlet UIView *hView; 
  2. IBOutlet UIView *vView; 
  3.   

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:

 
 
  1. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
  2.         if (interfaceOrientation==UIInterfaceOrientationLandscapeLeft) { 
  3.             //zuo 
  4.             self.view=self.hView; 
  5.            } 
  6.         if (interfaceOrientation==UIInterfaceOrientationLandscapeRight) { 
  7.             //you 
  8.             self.view=self.hView; 
  9.           } 
  10.         if (interfaceOrientation==UIInterfaceOrientationPortrait) { 
  11.             //shang 
  12.             self.view=self.vView; 
  13.            } 
  14.         if (interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) { 
  15.             //xia 
  16.             self.view=self.vView; 
  17.           } 
  18.         return YES; 
  19.     } 
  20.   

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

 
 
  1. -(NSUInteger) supportedInterfaceOrientations {
  2. 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 ).
  3. }
  4.  
  5. -(BOOL) shouldAutorotate {
  6. Return YES; // supports screen Conversion
  7. }

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.

 
 
  1. -(NSUInteger) supportedInterfaceOrientations {
  2. 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 ).
  3. }

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.

Related Article

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.