iOS横竖屏解决方案

来源:互联网 发布:2017七天网络阅卷查分 编辑:程序博客网 时间:2024/05/17 06:26

ios横竖屏的效果是不相同的,所以我们在开发中如果允许屏幕横竖屏间的切换,那么我们就要调整视图的布局。利用Interface Builder开发,我们可以快速的拖拽出合适的界面布局。

AD:2014WOT全球软件技术峰会北京站 课程视频发布

ios横竖屏的效果是不相同的,所以我们在开发中如果允许屏幕横竖屏间的切换,那么我们就要调整视图的布局。利用Interface Builder开发,我们可以快速的拖拽出合适的界面布局,但是屏幕自动切换布局不能很好的适配,下图是,没有做任何调整的状态下,实现的横竖屏切换,可以看到界面不是很美观。

image39.pngimage40.png

目前我所知的实现ios横竖屏切换的解决方案共有三种:

1.利用Interface Builder适配器自动适配调整界面。

2.在横竖屏切换时,每个控件重新布局。

3.利用Interface Builder创建两个视图,横屏时切换到横屏视图,竖屏时切换到竖屏视图。

在ios中,横竖屏切换时,会调用下面函数:

  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.   

返回yes表示切换屏幕,返回no是不能向相应的方向切换视图。

下面分别介绍一下三种方法,第一种方法最简单,但是效果是最差的,我们只需用Interface bulider修改相应的属性即可。实现的效果如下:
image41.pngimage42.png

实现的方法:

image43.png

选中控件,按command+3,上图红框部分的红线表示距离不能自动适配,要是虚线表示距离可以自动适配。我们选择可以自动适配,最后的结果就如上图。

第二种方法:

第二种方法是相应的控件和代码相关联:

代码:

  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.   

和IB相关联:

更改每一个控件的布局:

  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.    } 

第三种方法是创建两个视图,下面看一下实现过程:

首先创建两个视图:

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

创建相应的@property方法.

然后在IB中在复制一个view。

image44.png

把一个视图做横屏时的布局,一个view做竖屏时的布局。把相应的view和相应的方法相连接,在设置一个默认视图为view。

下面就是代码实现:

  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.   

实现的效果如下:

image45.pngimage46.png

上述就是我目前知道的三种横竖屏解决方案,我们可以看到第三种比较简单,但是编写比较麻烦,实现复杂逻辑比较麻烦,第二种方法实现起来不直观,调试比较麻烦,但是效果最好。

源代码:http://easymorse-iphone.googlecode.com/svn/trunk/ipad.demoo/

ios6.0横竖屏切换问题解决

this class is not key value coding-compliant for the key

ios5里面的旋转方法ios6里面确实掉不到了,但是还是可以用的。

首先,在app的主界面(也就是自己的主ViewController.m)里面加上

  1. -(NSUInteger)supportedInterfaceOrientations{ 
  2.     return UIInterfaceOrientationMaskAllButUpsideDown;//这里返回哪个值,就看你想支持那几个方向了。这里必须和后面plist文件里面的一致(我感觉是这样的)。 
  3.  
  4. - (BOOL)shouldAutorotate { 
  5.     return YES;//支持转屏 
  6.   

这两个函数。

然后在plist文件里面找到Supported interface orientations (iPad)选项,添加你想支持的方向,都有提示的。

然后问题就解决了。

也许我描述的还有问题,希望你能指正。谢谢了。

  1. -(NSUInteger)supportedInterfaceOrientations{ 
  2.     return UIInterfaceOrientationMaskAllButUpsideDown;//这里返回哪个值,就看你想支持那几个方向了。这里必须和后面plist文件里面的一致(我感觉是这样的)。 
  3.   

这里的设置会覆盖掉plist中的值

还有需要注意:mainViewController要设置为window的rootViewController,addSubView上去可能存在问题。并且上面的所有subViewController都会受到rootViewController支持朝向的影响

0 0
原创粉丝点击