在ipad應(yīng)用開(kāi)發(fā)時(shí)如何讓設(shè)備只支持橫屏(landscape)模式,我做了多次嘗試,并沒(méi)有發(fā)現(xiàn)比較簡(jiǎn)捷的設(shè)置方法。我嘗試了大概大概3種方式。
1、通過(guò)XCode設(shè)置“iPad Deployment info”,只選擇橫屏左和橫屏右,部署測(cè)試后并沒(méi)有生效,這種方法實(shí)質(zhì)是在xxx_info.plist項(xiàng)目配置文件中添加如下信息:
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
2、通過(guò)對(duì)每個(gè)nib文件在IB中設(shè)置orientation為landscape,此法也不生效。
3、重載shouldAutorotateToInterfaceOrientation:方法,這種方式是可行的。具體如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return ((interfaceOrientation ==UIDeviceOrientationLandscapeLeft)||(interfaceOrientation ==UIDeviceOrientationLandscapeRight));
}
如果第一種方式生效,那么比較完美。雖然第三種方式可以完全滿足橫屏的需求,但是實(shí)現(xiàn)起來(lái)比較stupid,需要在每個(gè)controller中都重載shouldAutorotateToInterfaceOrientation:方法,當(dāng)然也可以通過(guò)擴(kuò)展UIViewController的方式來(lái)避免重復(fù)勞動(dòng)。但是感覺(jué)也有點(diǎn)不太直接,期待有人指出sdk本身是否就有簡(jiǎn)捷方式支持。
iPad的界面布局好多時(shí)候都要做兩套------橫屏和豎屏,但在界面切換時(shí),該讓哪個(gè)布局顯示就要判斷了,有多種方法,我記錄下我用的一種,感覺(jué)比較方便:
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];//這個(gè)函數(shù)用來(lái)獲取當(dāng)前設(shè)備的方向,
- (void)orientationChanged
{
//UIView *ftView = [self.view viewWithTag:200];
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)//判斷左右
{
//界面的新布局
}
if([[UIDevice currentDevice] orientation] == UIInterfaceOrientationPortrait )//我有個(gè)方向不支持,如果想都支持那就不要這個(gè)if條件就行了
{
//界面的新布局
}
}
iOS 5 上真機(jī)測(cè)試過(guò)了。很方便,用過(guò)后才能體會(huì)到,這里不多說(shuō)了!
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注