項(xiàng)目需求:
1 豎屏狀態(tài),想在 present 的時(shí)候?yàn)闄M屏
2 橫屏狀態(tài)鸵闪, present的時(shí)候 為豎屏狀態(tài).
我只是以第一個(gè)需求為例梯皿,第二種情況,道理相同奖唯。
- 無需強(qiáng)行選中 Device Orientation 中的 Portrait 選項(xiàng) 如圖1,當(dāng)然選中也是可以的惨缆。
2.在appdelegate 中設(shè)置標(biāo)識(shí)符,或者枚舉類型臭埋,判斷當(dāng)前屏幕支持的屏幕 orientation 方向踪央。
// AppDelegate.h
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSUInteger, ViewDirection) {
ViewDirectionAll = 0,
ViewDirectionLandscape,
ViewDirectionPortrait,
};
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,assign) ViewDirection viewDirection;
@end
然后在.m文件中
// AppDelegate.m
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (self.viewDirection == ViewDirectionAll){
return UIInterfaceOrientationMaskAllButUpsideDown;
} else if (self.viewDirection == ViewDirectionLandscape){
return UIInterfaceOrientationMaskLandscape;
} else if ( self.viewDirection == ViewDirectionPortrait) {
return UIInterfaceOrientationMaskPortrait;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
3 .1先實(shí)現(xiàn) present 部分, 如果是跳轉(zhuǎn)到 navigationController 的rootViewController 中 需要在自定義navigationController 在.m中添加如下代碼
// @interface NavViewController : UINavigationController
// NavViewController.m
- (BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
// return UIInterfaceOrientationPortrait;
return UIInterfaceOrientationLandscapeLeft|UIDeviceOrientationLandscapeRight;
}
3.2 跳轉(zhuǎn)實(shí)現(xiàn)瓢阴。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
WebViewController *webVC = [[WebViewController alloc] initWithNibName:NSStringFromClass([WebViewController class]) bundle:nil];
NavViewController * nav = [[NavViewController alloc] initWithRootViewController:webVC];
[self presentViewController:nav animated:YES completion:nil];
}
4 特殊問題畅蹂,如果 是橫屏狀態(tài) 用到alertviewcontroller
時(shí)。會(huì)crash
荣恐。解決辦法液斜,寫一個(gè)分類.
#import "UIAlertController+MMAlertController.h"
@implementation UIAlertController (MMAlertController)
- (BOOL)shouldAutorotate{
return NO;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
@end
5 特殊問題2累贤,如果是橫屏狀態(tài),跳轉(zhuǎn)到豎屏狀態(tài)少漆,而豎屏又push 到另一個(gè)vc中臼膏,一定要在另一個(gè)vc中重寫 上三方法。不然會(huì)導(dǎo)致 status bar
橫過來示损。