iphone的橫屏豎屏針對iOS系統版本分為兩種開發方式: 一種是iOS 6之前的使用模式 一種是iOS6的新模式. 兩者的區別還是蠻大的.
1:iOS6之前通常使用 shouldAutorotateToInterfaceOrientation 來單獨控制某個UIViewController的方向,需要哪個viewController支持旋轉,只需要重寫shouldAutorotateToInterfaceOrientation方法。如下示例,設置以后,屏幕被旋轉時只支持橫屏轉換:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}
iOS6之后使用如下兩個方法控制自動旋轉,分別是:
- (BOOL)shouldAutorotate
{
NSLog(@"讓不讓我旋轉?");
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
NSLog(@"讓我旋轉哪些方向");
return UIInterfaceOrientationMaskAllButUpsideDown;
}
那么在自動旋轉觸發后,系統會自動調用另外兩個方法:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
NSLog(@"將要旋轉了?");
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
NSLog(@"如果讓我旋轉,我已經旋轉完了!");
}
2:讓程序第一次啟動時立刻顯示橫屏還是豎屏的方式
如果是iOS6之前,下面設置的設備支持方向可在應用里面再被修改
如果是iOS6以后,會做為硬性條件,也就是如果設置了以后,應用里面的代碼也無法再使用這個方向
3:傳說中的私有API實現切換ViewController強制橫屏的方式
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:)
withObject:(id)UIInterfaceOrientationLandscapeRight];
}
4:使用xib進行界面設計時,改變xib的橫豎顯示方式