當(dāng)iphone的某一方向(一般是豎屏)鎖定沒有開啟的時候裆悄,當(dāng)手機橫放后墨辛,沒有寫死程序支持方向的程序或者支持橫屏的程序都會翻轉(zhuǎn)
以上是廢話
但是當(dāng)豎屏鎖定開啟的時候躲因,某一時刻當(dāng)程序需要強制橫屏展示的時候棘伴,就比較難受了晌区,這個坑我開始做的時候百度到了一堆不知所云的答案摩骨,在爬了數(shù)不清的文檔之后差不多摸清楚了里面的坑了
首先是Target里的General里有Deviece Orientation的設(shè)置,這里一般默認(rèn)是選中Portrait的朗若,既默認(rèn)豎屏
要注意的是這個設(shè)置的是全局的方向恼五,如果只是部分頁面需要改變方向的話其實這個地方完全可以不用設(shè)置,我們只要在對應(yīng)的controller里通過代碼來設(shè)置
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
不過這里設(shè)置的東西當(dāng)豎屏鎖定開啟之后就沒什么用了哭懈,為了解決程序強制橫屏問題可以有下面兩個思路
第一灾馒,一個navigation推出來的controller是一定和這個導(dǎo)航一個方向的,這樣我們就可以做一個橫向的navigation遣总,用這個navigation來push我們想要橫屏的controller睬罗,這樣就很輕松的實現(xiàn)了某一頁面橫屏的要求,并且這種方法的好處是不會和彈出的鍵盤沖突旭斥,沒有那些莫名其妙的bug容达,缺點就是生成新的navigation的時候動畫需要自己來寫,生成的navigation里的和方向有關(guān)方法只重要寫下面三個就可以了
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate
{
return YES;
}
之后就用自己生成的navigation彈出controller就可以了垂券,代碼如下
ZCNavigationController *nav = [[ZCNavigationController alloc]init];
nav.navigationBarHidden = YES;
[nav addChildViewController:web];
[self presentViewController:nav animated:YES completion:nil];
第二種思路就是強制讓設(shè)備轉(zhuǎn)向花盐,,由豎屏鎖定變成橫屏鎖定,這個缺點就是貌似會導(dǎo)致閃退(反正我沒有遇到)以及輸入法bug
首先就是要在appdelegate里重寫下面的方法
- (UIInterfaceOrientationMask )application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (allowRotation == 1) {
return UIInterfaceOrientationMaskLandscape;
}
else
{
return (UIInterfaceOrientationMaskPortrait);
}
}
allowRotation是自己定義的一個變量算芯,用來判斷是要橫屏還是豎屏用的柒昏,接下來在要橫屏的頁面里加入以下代碼
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = 1;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];//這句話是防止手動先把設(shè)備置為橫屏,導(dǎo)致下面的語句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];
一般我會寫到viewwillappear里面,同理也祠,在viewwilldisappear方法里將屏幕回正
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.allowRotation = 0;
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationLandscapeLeft] forKey:@"orientation"];//這句話是防止手動先把設(shè)備置為豎屏,導(dǎo)致下面的語句失效.
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIDeviceOrientationPortrait] forKey:@"orientation"];
這里將我暫時遇到的坑說一下
ios9以上要加上如下代碼昙楚,否則你會發(fā)現(xiàn)你彈出的輸入鍵盤根本不旋轉(zhuǎn)
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 9.0){ if ([self.mytext isFirstResponder]) { [self.mytext resignFirstResponder]; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog(@"進來啦"); [self.mytext becomeFirstResponder]; }); }
當(dāng)然你也可以先取消鍵盤的響應(yīng),等轉(zhuǎn)向完成后再彈出鍵盤一樣也是可以的