首先萍鲸,確保APP支持屏幕旋轉(zhuǎn)蛛枚,具體怎么支持暖哨,去網(wǎng)上查詢一下
然后,在對應(yīng)的類中進(jìn)行如下操作
pragma mark 轉(zhuǎn)屏方法重寫
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return [self.viewControllers.lastObject supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return [self.viewControllers.lastObject preferredInterfaceOrientationForPresentation];
}
-(BOOL)shouldAutorotate {
return self.visibleViewController.shouldAutorotate;
}
最后墨状,在你不想轉(zhuǎn)屏切換的的ViewController上重寫以下方法:
pragma mark 轉(zhuǎn)屏方法 不允許轉(zhuǎn)屏
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait ;
}
- (BOOL)shouldAutorotate{
return NO;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return NO;
}
在你想轉(zhuǎn)屏切換的的ViewController上可以照這樣重寫(允許左右橫屏以及豎屏):
(BOOL)shouldAutorotate {
return YES;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
另外,在的ViewController中對于轉(zhuǎn)屏事件可以參見下面的方法進(jìn)行捕獲:-
(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
//計(jì)算旋轉(zhuǎn)之后的寬度并賦值
CGSize screen = [UIScreen mainScreen].bounds.size;
//界面處理邏輯
self.lineChartView.frame = CGRectMake(0, 30, screen.width, 200.0);
//動(dòng)畫播放完成之后
if(screen.width > screen.height ){NSLog(@"橫屏");
}else{
NSLog(@"豎屏");
}} completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
NSLog(@"動(dòng)畫播放完之后處理");
}];
}
區(qū)分當(dāng)前屏幕是否為橫豎屏的狀態(tài)菲饼,其實(shí)通過判斷當(dāng)前屏幕的寬高來決定是不是橫屏或者豎屏:
**豎屏?xí)r:
**寬<高
**橫屏?xí)r:
**寬>高