iOS

您所在的位置:网站首页 ipadmini横竖屏怎么设置 iOS

iOS

2024-03-19 20:18| 来源: 网络整理| 查看: 265

1.支持横竖屏,但默认打开时是横屏

 

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];

   //Landscape开头的是横屏

 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

//支持横屏     return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) || (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft); }

 

让模拟器横屏,然后加一个视图控制控制它的转向为横屏

 

2.仅仅支持横屏,不支持竖屏

 

众所周知,iOS APP默认支持四个方向,即Portrait、PortraitUpsideDown、LandscapeLeft、LandscapeRight。在有些画图类、游戏类应用中,需要限制APP仅支持横屏或仅支持竖屏,这就需要在程序中禁止支持其他两个方向。下面以横屏应用为例介绍一下如何进行设置(xcode版本为4.6.1):

首先,在项目的info.plist中去掉竖屏的两个方向,保留横屏方向,如下图所示

然后,在项目中的UINavigationController子类中添加如下代码

 

[plain] view plaincopy

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  {      return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||                 interfaceOrientation == UIInterfaceOrientationLandscapeRight );   }  

对于仅支持横屏的应用,需要注意下面的问题:

iOS模拟器有个在viewDidLoad时,不论你模拟器的方向是什么方向,它默认一开始总是Portrait竖屏,因此width跟height也是竖屏的数值。

解决方法:一种方法是通过view的autoresizingMask属性对width以及height进行设置,设备转向时能够保证这些view的width、height按照竖屏的标准来调整;另一种方法是直接在viewDidLoad中不使用autoresizingMask,而是交换width和height的值,再按照横屏去布局。

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

3.如何让设备只支持横屏(landscape)模式

 

在iPad应用开发时如何让设备只支持横屏(landscape)模式,我做了多次尝试,并没有发现比较简捷的设置方法。我尝试了大概大概3种方式。

1、通过XCode设置“iPad Deployment info”,只选择横屏左和横屏右,部署测试后并没有生效,这种方法实质是在xxx_info.plist项目配置文件中添加如下信息:

 

UISupportedInterfaceOrientations~ipad UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight 2、通过对每个nib文件在IB中设置orientation为landscape,此法也不生效。 3、重载shouldAutorotateToInterfaceOrientation:方法,这种方式是可行的。具体如下: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {     // Return YES for supported orientations return ((interfaceOrientation ==UIDeviceOrientationLandscapeLeft)||(interfaceOrientation ==UIDeviceOrientationLandscapeRight)); } 如果第一种方式生效,那么比较完美。虽然第三种方式可以完全满足横屏的需求,但是实现起来比较stupid,需要在每个controller中都重载shouldAutorotateToInterfaceOrientation:方法,当然也可以通过扩展UIViewController的方式来避免重复劳动。但是感觉也有点不太直接,期待有人指出sdk本身是否就有简捷方式支持。 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3