簡體   English   中英

如何鎖定設備上更改方向?

[英]How to lock changing orientation on device?

如何鎖定設備上更改方向?

我嘗試使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return UIInterfaceOrientationLandscapeRight;
}

和這個

- (void)viewDidAppear:(BOOL)animated
{
    [self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
    [super viewDidAppear:animated];
}

但是當我使用設備時沒有任何效果。 當應用在模擬器上運行時,效果很好。

模擬器具有iOS 6和設備5.1.1。 我做錯了什么?

嘗試這個:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

該方法返回“是”或“否”,並回答問題“我應該旋轉到該方向:interfaceOrientation”。 因此,您應該返回YES或NO- UIInterfaceOrientationLandscapeRight不是BOOL。 但是您可以使用比較來返回布爾值,如上所述。

打開您的項目,選擇項目文件,轉到“摘要”部分,然后在“支持的界面方向”子部分中設置所需的界面方向。 祝您好運!(適用於iOS 6.x)

在您的項目.plist文件中,添加支持的界面方向

並將項目設置為Landscape(主頁按鈕)

如果您只想對1個ViewController進行操作,則可以通過以下任一方式進行操作

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

//deprecated
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {
return self.interfaceOrientation == UIInterfaceOrientationPortrait;
}

如果要為整個應用程序設置它,請確保更改Info.plist文件並添加Supported Interface Orientations

您只需在.m文件中添加以下行

#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

然后,您必須在.m文件中添加以下方法

#ifdef IOS_OLDER_THAN_6

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#endif

#ifdef IOS_NEWER_OR_EQUAL_TO_6

- (NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscapeRight;
}
#endif

對於iOS 6支持包括以下兩種方法

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

還請注意,這將適用於xCode 4.5 +

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM