Detailed explanation of iPad horizontal and vertical screen switching Solution

Source: Internet
Author: User

DetailsHorizontal and vertical screen switching for iPadThe solution is the content to be introduced in this article. Let's look at the content first. BecauseIpadOfPortrait ScreenDifferent, so good applications,Portrait ScreenThe page layout is also different. Then you needPortrait Screen. First read onePortrait ScreenDifferent la S.

 

The above two screenshots are screenshots from the same interface. As you can see,Horizontal and vertical layoutThe displayed content is the same, but the interface layout is different. To achieve the above layout, we mainly use the layoutSubviews method in UIView. When UIView is set to automatically adapt to the screen, when the user rotates the device, the layoutSubviews method is called. We only need to rewrite this method and then determine the direction of the user's screen. You can adjust the location of each space.

The following is the simplest prototype for implementing the above interface:

First, the analysis shows that the left side is an image and the right side is an image and Text View. The following is an example of adding a word to the right of the Left view.

The example is as follows:

 

The text and green on the right are encapsulated in a subview.

The entire layout is that I added a ContentView to the main view and added an ArticleView TO THE ContentView view.

The xib files of ArticleView and ContentView are opened.

In ContentView, override the layoutSubviews method, and then judge the landscape of the current view based on the direction of the stausbar. Code:

 
 
  1. -(Void) layoutSubviews {
  2. [Super layoutSubviews];
  3. UIDeviceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  4. If (interfaceOrientation = UIDeviceOrientationPortrait | interfaceOrientation = UIDeviceOrientationPortraitUpsideDown ){
  5. // When the screen is flipped to the portrait
  6. [Self setVerticalFrame];
  7. } Else if (interfaceOrientation = UIDeviceOrientationLandscapeLeft | interfaceOrientation = UIDeviceOrientationLandscapeRight ){
  8. // When the screen is flipped to a landscape
  9. [Self setHorizontalFrame];
  10. }
  11. }
  12.  
  13. -(Void) setVerticalFrame
  14. {
  15. NSLog (@ "portrait screen ");
  16. [TitleLable setFrame: CGRectMake (283, 0,239, 83)];
  17. [LeftView setFrame: CGRectMake (38,102,384,272)];
  18. [RightView setFrame: CGRectMake (450,102,282,198)];
  19. }
  20.  
  21. -(Void) setHorizontalFrame
  22. {
  23. NSLog (@ "Landscape ");
  24. [TitleLable setFrame: CGRectMake (183, 0,239, 83)];
  25. [LeftView setFrame: CGRectMake (168,122,384,272)];
  26. [RightView setFrame: CGRectMake (650,122,282,198)];
  27. }

In the specific horizontal and vertical screen method, set the coordinates of each component.

Next, add the ArticleView to ContentView.

 
 
  1. -(id)initWithCoder:(NSCoder *)aDecoder   
  2. {   
  3.     if ((self = [super initWithCoder:aDecoder])) {   
  4.  
  5.         NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil];   
  6.         rightView=[arrayContentView objectAtIndex:0];   
  7.         [self addSubview:rightView];   
  8.     }   
  9.     return self;   

Because I use xib, the initialization method is initWithCoder. Add a new view here.

You can also set the coordinates of the corresponding space on the horizontal and vertical screens in the ArticleView.

 
 
  1. -(Void) layoutSubviews {
  2. [Super layoutSubviews];
  3. UIDeviceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  4. CGRect rect = self. frame;
  5. Rect. size. size = 282;
  6. Rect. size. height = 198;
  7. [Self setFrame: rect];
  8. If (interfaceOrientation = UIDeviceOrientationPortrait | interfaceOrientation = UIDeviceOrientationPortraitUpsideDown ){
  9. // When the screen is flipped to the portrait
  10. [Self setVerticalFrame];
  11. } Else if (interfaceOrientation = UIDeviceOrientationLandscapeLeft | interfaceOrientation = UIDeviceOrientationLandscapeRight ){
  12. // When the screen is flipped to a landscape
  13. [Self setHorizontalFrame];
  14. }
  15. }
  16.  
  17. -(Void) setVerticalFrame
  18. {
  19. NSLog (@ "portrait screen ");
  20. [ContentView setFrame: CGRectMake (12, 6,250,125)];
  21. [TextLable setFrame: CGRectMake (50,139,182, 39)];
  22. }
  23.  
  24. -(Void) setHorizontalFrame
  25. {
  26. NSLog (@ "Landscape ");
  27. [ContentView setFrame: CGRectMake (12, 6,106,158)];
  28. [TextLable setFrame: CGRectMake (135, 11,147, 39)];
  29. }

Source code: http://easymorse-iphone.googlecode.com/svn/trunk/IpadLayOut/

Summary: DetailsHorizontal and vertical screen switching for iPadAfter the solution is introducedIpadScreenSwitchIs it clear? Finally, I hope this article will help you.

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.