第一步: 要想App能夠支持多個方向, 需要在創(chuàng)建工程時設(shè)置支持的方向: 在General-->Deployment Info-->Device Orientation
中進行設(shè)置
第二步: 實現(xiàn)上面的設(shè)置, 下面需要通過相應(yīng)的方法來設(shè)置是否支持旋轉(zhuǎn) + 旋轉(zhuǎn)方向等問題.
主要方法如下:
#1. 是否自動旋轉(zhuǎn),返回YES可以自動旋轉(zhuǎn) (假如該方法返回的是NO, 那么你再做什么設(shè)置, 工程中都不會有自動旋轉(zhuǎn)屏幕的效果了)
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;
#2. 返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
#3. 這個是返回優(yōu)先支持的方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;```
*如果設(shè)置的當前頁面, 是App的根視圖控制器, 那么實現(xiàn)以上兩個步驟就能完成自動旋轉(zhuǎn)了, 上面的主要方法也都是UIViewController中的實例方法.
***
你會發(fā)現(xiàn), 只有App啟動時出現(xiàn)的第一個界面(根視圖)內(nèi)重寫上面的方法, 會有效果. 而在其他的界面重寫之后并沒有實現(xiàn)我們想要的翻轉(zhuǎn). (* 由此證明, 這些設(shè)置必須是影響了根視圖中的設(shè)置, 才會有效果. 所以如果想讓根視圖以外的視圖也支持旋轉(zhuǎn), 需要告訴視圖, 在視圖中修改響應(yīng)的設(shè)置)
##例1. 當前controller是根視圖控制器
那么很簡單, 我們直接在當前Controller中重寫上面的兩個方法即可:
1. 支持自動旋轉(zhuǎn)
- (BOOL)shouldAutorotate{
return YES;
}
2. 值傳的旋轉(zhuǎn)方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//支持除了倒置的所有方向
return UIInterfaceOrientationMaskAllButUpsideDown;
}```
例2. App的根視圖控制器是UINavigationController
這是開發(fā)中很常用的, 那么下面介紹怎樣在navigationController中做控制:
第一種方法:
這種方式, 主要用在當push到某個視圖控制器時, 要求該視圖: 支持自動旋轉(zhuǎn)或者強制旋轉(zhuǎn)為橫屏.
# 下面的方法實現(xiàn)過程中, 主要用到的是navigationController中的viewControllers屬性, 這是一個數(shù)組, 數(shù)組的最后一個元素就是最后push到的controller.
-(BOOL)shouldAutorotate {
return [[self.viewControllers lastObject] shouldAutorotate];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
下面在最后push到的controller中調(diào)用上面三個方法
-(BOOL)shouldAutorotate{
return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortraitUpsideDown;
}```
其實這樣也就是告訴跟視圖控制器,這個界面我要特殊設(shè)置一下
#####第二種方法:
這種方法是對特定的控制器進行設(shè). 例如我需要只設(shè)定firstVC為支持橫屏, 只需要修改navigationController中的代碼:
-(BOOL)shouldAutorotate{
//判斷子控制器是不是firstVC
if ([[self.viewControllers lastObject]isKindOfClass:[FirstViewController class]]) {
//如果是則返回Yes
return YES;
}
//如果是其他視圖控制器, 則返回No
return NO;
}
-
(UIInterfaceOrientationMask)supportedInterfaceOrientations{
if ([[self.viewControllers lastObject]isKindOfClass:[FirstViewController class]]) {
//支持向左翻轉(zhuǎn)
return UIInterfaceOrientationMaskLandscapeLeft;
}
//僅支持豎屏
return UIInterfaceOrientationMaskPortrait;
} -
(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if ([[self.viewControllers lastObject]isKindOfClass:[FirstViewController class]]) {
return UIInterfaceOrientationLandscapeLeft;
}
return UIInterfaceOrientationPortrait;
}```
這樣的好處是: 可以控制指定的控制器, 也不用在指定的控制器內(nèi)重寫上面的方法. 但如果需要自動翻轉(zhuǎn)的視圖比較多的話, 判斷比較麻煩
例3: 根視圖控制器是UITabBarController
首先,設(shè)置一個全局的BOOL值,用于接收通知發(fā)送的參數(shù):
#import <UIKit/UIKit.h>
@interface BaseTabBar : UITabBarController{
BOOL shouldAutorotate;
}
@end
然后注冊一個通知:
//注冊旋轉(zhuǎn)屏幕的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(autorotateInterface:) name:@"InterfaceOrientationNotification" object:nil];
實現(xiàn)通知方法:
-(void)autorotateInterface:(NSNotification *)notifition{
shouldAutorotate = [notifition.object boolValue];
}
然后在重寫的方法里加入判斷:
-(BOOL)shouldAutorotate {
if (!shouldAutorotate) {
return NO;
}
else{
return YES;
}
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (!shouldAutorotate) {
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
這里我直接將支持的方向?qū)懰懒?只是判斷了一下是否支持自動旋轉(zhuǎn)屏幕,如果需要將支持的方向傳過來,可以修改通知攜帶的參數(shù);
最后在需要自動轉(zhuǎn)屏的控制器內(nèi)發(fā)送通知:
[[NSNotificationCenter defaultCenter] postNotificationName:@"InterfaceOrientationNotification" object:@"YES"];```
ending ......
#強制轉(zhuǎn)換:
強制旋轉(zhuǎn)屏幕的方法,可以在進入某個視圖時,強制轉(zhuǎn)成需要的屏幕方向,用的比較多的是在一個豎屏的應(yīng)用中強制轉(zhuǎn)換某一個界面為橫屏(例如播放視頻):
-
(void)orientationToPortrait:(UIInterfaceOrientation)orientation {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];//前兩個參數(shù)已被target和selector占用 [invocation invoke];
}```
使用的時候,只需要把你需要旋轉(zhuǎn)的方向傳過去即可!
有一點需要注意:從A進入B的時候,把B強制轉(zhuǎn)換成橫屏,返回的時候,需要在A出現(xiàn)的時候再轉(zhuǎn)換為原來的方向,不然會有問題;個人建議可以在B的viewWillAppear調(diào)用這個方法,轉(zhuǎn)換屏幕(例如轉(zhuǎn)換為橫屏),然后在A的viewWillAppear中轉(zhuǎn)換回來;
文/流火緋瞳(簡書作者)
原文鏈接:http://www.reibang.com/p/650ba0ff626b