IOS6屏幕自動(dòng)旋轉(zhuǎn)設(shè)置測試
結(jié)論如下:
1> 如果是在rootViewController中添加了方向控制,則他的子視圖都默認(rèn)繼承rootViewController的方向控制
子視圖的解釋是:
1 如果rootviewcontroller是普通的uiviewcontroller,則通過presentViewController方法進(jìn)來的也都叫做子視圖,同時(shí)這種方法也受到info.plist的限制,他們是繼承關(guān)系,也可以覆蓋父類設(shè)置,但是覆蓋范圍只能越來越小
2 >如果rootViewController是導(dǎo)航UINavigationController,又有2種情況:
1通過push,進(jìn)來的各種子頁面,則必需在navigation bar 這個(gè)跟上設(shè)置旋轉(zhuǎn),這種可能只能通過category來操作它了,而且各個(gè)子頁面也繼承了父類設(shè)置,子頁面自己不能單獨(dú)設(shè)定,除非UINavigationController把控制權(quán)交給了每個(gè)子頁面,則子頁面可以單獨(dú)設(shè)定,但是和父類的設(shè)定也是繼承關(guān)系,也可以覆蓋父類設(shè)置,但是覆蓋范圍只能越來越小
2 presentViewController進(jìn)來的各種子頁面,則只能自己單獨(dú)設(shè)定了,和UINavigationController設(shè)定沒有任何繼承關(guān)系,也沒有任何聯(lián)系
3 >可以在uiWindow上直接添加子視圖,這樣的視圖不受info.plist限制
測試結(jié)果:
1>plist中,supported interface Orientations中,程序打開默認(rèn)的方向,與這里面設(shè)置的先后順序無關(guān)!和打開時(shí)候,設(shè)備當(dāng)時(shí)的方向有關(guān)
2> 如果在程序中沒有做任何方向設(shè)置,則單純的只在supported interface Orientations設(shè)置是對所有頁面都生效的
3>info.plist設(shè)置所有方向都支持,然后在rootViewController中設(shè)置shouldAutorotate為NO,則根本就不會(huì)旋轉(zhuǎn),而且默認(rèn)打開方向也是豎屏的,說明這個(gè)設(shè)置會(huì)覆蓋info.plist設(shè)置
4>info.plist設(shè)置所有方向都支持,然后在rootViewController中設(shè)置shouldAutorotate為YES,而且設(shè)置
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
測試結(jié)果:按鈕朝上時(shí)候,屏幕不再旋轉(zhuǎn)
3,4說明程序里的設(shè)置會(huì)覆蓋info.plist的設(shè)置
5>設(shè)定info.plist只支持橫屏(landscape Left,landscape Right),然后在rootViewController中設(shè)置shouldAutorotate為YES,而且設(shè)置
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
這時(shí)候發(fā)現(xiàn)屏幕只能在橫屏模式下工作
3,4,5三點(diǎn)測試說明,info.plist設(shè)置是生效的,如果info.plist旋轉(zhuǎn)方向設(shè)置的范圍大,則可以在代碼中設(shè)置更小的范圍,如果info.plist設(shè)置的范圍小,在代碼中再擴(kuò)大范圍,也是無效的
6>在5的基礎(chǔ)上,在第一個(gè)頁面加一個(gè)按鈕,點(diǎn)擊進(jìn)入第二個(gè)頁面(SecondPage)
1.第二個(gè)頁面什么都不設(shè)置,發(fā)現(xiàn)第二個(gè)頁面的旋轉(zhuǎn),繼承了rootViewController的設(shè)置,只能橫屏
- SecondPage設(shè)置shouldAutorotate為NO,發(fā)現(xiàn)第一個(gè)頁面可以正常旋轉(zhuǎn),到第二個(gè)頁面就不旋轉(zhuǎn)了,說明接下來的設(shè)置還是會(huì)覆蓋rootViewController的設(shè)置!
- SecondPage設(shè)置shouldAutorotate為YES,然后設(shè)置
- -(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskLandscapeLeft;}
發(fā)現(xiàn)第一個(gè)頁面可以正常旋轉(zhuǎn),到第二個(gè)頁面只能是left方向
- 在4點(diǎn)基礎(chǔ)上,設(shè)置-(NSUInteger)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAll;}發(fā)現(xiàn)第二個(gè)頁面只能左右,并不能4個(gè)方向都任意旋轉(zhuǎn)
2,3,4三小點(diǎn),說明了present的情況下,子頁面會(huì)繼承父頁面的設(shè)置,同時(shí)子頁面也可以覆蓋父頁面的設(shè)置,但是覆蓋的范圍只能是越來越小,不能越來越大
上面測試的是普通的rootViewController,在正常present的情況下,如果是UINavigationController下,用push呢
開始測試UINavigationController的push情況
上面測試結(jié)果不一樣了,發(fā)現(xiàn)凡是push進(jìn)來的頁面,包括一開始在appDelegate中初始化的UINavigationController
第一個(gè)controller,類似這樣
[[UINavigationController alloc] initWithRootViewController:firstPage
在firstPage,和secondPage里設(shè)置旋轉(zhuǎn)都是無效的
說明了IOS6旋轉(zhuǎn)只能在根controller下旋轉(zhuǎn), UINavigationController這種情況下,根是UINavigationController,不是firstPage!
因此我們添加UINavigationController的category來來解決旋轉(zhuǎn)設(shè)置
我們添加一個(gè)UINavigationController+Rotate文件
然后在里面設(shè)置
-(BOOL)shouldAutorotate
{
return NO;
}
發(fā)現(xiàn)不旋轉(zhuǎn)了,只有豎屏了,說明生效了
然后我們在info.plist里面設(shè)置支持所有方向,然后在UINavigationController+Rotate.m的里面設(shè)置
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
發(fā)現(xiàn)只有橫屏生效,說明同樣的,這個(gè)設(shè)置覆蓋了info.plist設(shè)置,并且只能越來越小范圍的覆蓋
繼續(xù)修改:
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
這樣設(shè)置,就相當(dāng)于把設(shè)置權(quán)給下面的具體子視圖了,因?yàn)槊總€(gè)子視圖可以根據(jù)自己的情況,自己設(shè)定自己的旋轉(zhuǎn)方向去
如果是UINavigationController下,用present添加子頁面呢?
如果是push情況下,如果UINavigationController+Rotate沒把具體控制權(quán)給子頁面,則子頁面設(shè)置都是無效的
但是如果用present添加子頁面,則子頁面里添加控制權(quán)就是生效的,而且不再繼承UINavigationController+Rotate的任何設(shè)置
結(jié)論:
普通的rootViewController,添加子頁面也就是通過present方法添加,這時(shí)候生效是這樣的
info.plist---> rootViewController--->subview的設(shè)置,他們的設(shè)置是繼承關(guān)系,但可以覆蓋,但是覆蓋設(shè)置的范圍只能越來越小
如果rootViewController是UINavigationController,UITabController,則有2種可能
1 push進(jìn)來的子頁面,這時(shí)候設(shè)置旋轉(zhuǎn)只能通過UINavigationController的Category來設(shè)定,子頁面自己設(shè)定無效.當(dāng)然UINavigationController的Category也可以把控制權(quán)交給具體的子頁面來設(shè)定,這種情況也是這樣的
info.plist---> UINavigationController--->subview,他們是繼承關(guān)系,并且范圍只能設(shè)定越來越小
2 present進(jìn)來的頁面,這時(shí)候present進(jìn)來的頁面不受UINavigationController設(shè)定的控制,也不再存在繼承關(guān)系,需要自己單獨(dú)設(shè)定是否旋轉(zhuǎn),以及旋轉(zhuǎn)方向
另外兼容ios5:
ios5屏幕旋轉(zhuǎn)比較簡單,沒有iofo.list的支持和限制,因此ios5下設(shè)置info.plist是無效的
ios5只需要在想旋轉(zhuǎn)的頁面,設(shè)置一個(gè)方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
}
即可,其中可旋轉(zhuǎn)的項(xiàng)有
UIInterfaceOrientationPortrait//垂直 UIInterfaceOrientationPortraitUpsideDown//倒轉(zhuǎn) UIInterfaceOrientationLandscapeLeft//home左 UIInterfaceOrientationLandscapeRight//home右
因此兼容ios5需要在頁面上分別進(jìn)行設(shè)置
歡迎學(xué)習(xí)IOS的朋友請加ios技術(shù)交流群:190956763,共同學(xué)習(xí),共同進(jìn)步!

浙公網(wǎng)安備 33010602011771號(hào)