在ipad應用開發時如何讓設備只支持橫屏(landscape)模式,我做了多次嘗試,并沒有發現比較簡捷的設置方法。我嘗試了大概大概3種方式。
1、通過XCode設置“iPad Deployment info”,只選擇橫屏左和橫屏右,部署測試后并沒有生效,這種方法實質是在xxx_info.plist項目配置文件中添加如下信息:
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
2、通過對每個nib文件在IB中設置orientation為landscape,此法也不生效。
3、重載shouldAutorotateToInterfaceOrientation:方法,這種方式是可行的。具體如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return ((interfaceOrientation ==UIDeviceOrientationLandscapeLeft)||(interfaceOrientation ==UIDeviceOrientationLandscapeRight));
}
如果第一種方式生效,那么比較完美。雖然第三種方式可以完全滿足橫屏的需求,但是實現起來比較stupid,需要在每個controller中都重載shouldAutorotateToInterfaceOrientation:方法,當然也可以通過擴展UIViewController的方式來避免重復勞動。但是感覺也有點不太直接,期待有人指出sdk本身是否就有簡捷方式支持。
iPad的界面布局好多時候都要做兩套------橫屏和豎屏,但在界面切換時,該讓哪個布局顯示就要判斷了,有多種方法,我記錄下我用的一種,感覺比較方便:
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
[nc addObserver:self //Add yourself as an observer
selector:@selector(orientationChanged)
name:UIDeviceOrientationDidChangeNotification
object:nil];//這個函數用來獲取當前設備的方向,
- (void)orientationChanged
{
//UIView *ftView = [self.view viewWithTag:200];
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)//判斷左右
{
//界面的新布局
}
if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait )//我有個方向不支持,如果想都支持那就不要這個if條件就行了
{
//界面的新布局
}
}
iOS 5 上真機測試過了。很方便,用過后才能體會到,這里不多說了!
新聞熱點
疑難解答