方向分兩種,一種是設備的方向,一種是視圖方向。設備方向有兩種方式可以改變,一個是通過重力加速計,即旋轉屏幕的方式去改變,一個是通過代碼
自然屏幕旋轉就會有多種方式,比如:通過旋轉window實現,通過旋轉view實現,通過旋轉layer實現,通過UIInterfaceOrientationMask實現。
一、直接設置 UIDevice 的 orientation(不推薦,可能被和諧)
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
}
二、所有頁面橫屏(可使用 Interface Builder工具設計界面)
// 配置plist
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
三、調整window的方向,這樣就不用每個view都改變了
// 重寫方法四中方法為不允許自動
UIApplication *application=[UIApplication sharedApplication];
[application setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI);
四、重寫方法(支持重力感應)
TabbarController中
#pragma mark - 控制旋轉屏幕
#pragma mark 支持旋轉的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}
#pragma mark 是否支持自動旋轉
- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
}
NavigationController中
#pragma mark - 控制旋轉屏幕
#pragma mark 支持旋轉的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
#pragma mark 是否支持自動旋轉
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
某個控制器支持旋轉(不支持則改變返回值)
#pragma mark - 控制旋轉屏幕
#pragma mark 支持旋轉的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
#pragma mark 是否支持自動旋轉
- (BOOL)shouldAutorotate
{
return YES;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
五、設備旋轉+視圖旋轉(最終我項目采用的方案)
#pragma mark - 控制旋轉屏幕
#pragma mark 1.旋轉設備
- (void)interfaceOrientation:(BOOL)rotate
{
UIInterfaceOrientation orientation = rotate ? UIInterfaceOrientationLandscapeRight : UIInterfaceOrientationPortrait;
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)])
{
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];
[invocation invoke];
}
[[UIApplication sharedApplication] setStatusBarOrientation:orientation];
[[UIApplication sharedApplication] setStatusBarHidden:rotate];
}
#pragma mark 2.旋轉視圖
- (void)rotateView:(BOOL)rotate
{
self.view.transform = CGAffineTransformMakeRotation(rotate ? M_PI_2 : 0);
self.view.bounds = [UIScreen mainScreen].bounds;
}
#pragma mark 3.重新布局
- (void)reLayoutContentViews
{
}
是否允許旋轉
Appdelagate.h中
@property (nonatomic,assign)NSInteger allowRotate;
Appdelegate.m中
#pragma mark - 控制旋轉屏幕
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (_allowRotate == 1)
{
return UIInterfaceOrientationMaskAll;
}
else
{
return (UIInterfaceOrientationMaskPortrait);
}
}
需要旋轉功能的控制器中
- (void)viewWillAppear:(BOOL)animated
{
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 1;
}
- (void)viewWillDisappear:(BOOL)animated
{
AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
delegate.allowRotate = 0;
// 防止pop到上一級界面仍然橫屏
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = UIInterfaceOrientationPortrait;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
}