[英]How can I lock orientation for a specific view an Objective-C iPhone app in iOS 8?
我只是想設置我的應用程序,以便在橫向模式下只能查看一個視圖。
我已經嘗試了從shouldAutorotate
到supportedInterfaceOrientation
到preferedInterfaceOrientationForPresentation
幾乎所有內容,並將[UIDevice currentDevice]
的方向設置為縱向。 我在 Info.plist 和項目的常規部分啟用了 Landscape。
首先在你的 info.plist 中制作你的應用程序肖像
在 AppDelegate 類中創建以下屬性:
@property BOOL restrictRotation;
然后在您的 AppDelegate.m 類中創建以下方法:
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
在您的 ViewController 中創建以下方法並在您想要允許橫向之前調用它。 (首先在 viewDidLoad 方法中使用 true 調用它以確保旋轉受到限制)
-(void) restrictRotation:(BOOL) restriction
{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = restriction;
}
像這樣:
[self restrictRotation:NO];
完成景觀視圖並取消后,立即調用:
[self restrictRotation:YES];
希望這能回答你的問題。
鎖定后也改變方向:
-(void)forceOrientation:(UIInterfaceOrientation)orientation{
[[UIDevice currentDevice]setValue:[NSNumber numberWithInteger:orientation]forKey:@"orientation"];
}
對於 iOS 6 及更高版本,在使用Gurtej Singh答案時清除 AppDelegate 中的警告,您可以將 AppDelegate 代碼替換為:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos)
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}
在您的應用程序委托中,您可以執行以下操作:
// Used to show a video in full screen
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int
{
if let presentedViewController = self.window?.rootViewController?.presentedViewController
{
if !presentedViewController.isBeingDismissed() && presentedViewController is MyLandscapeModeViewController // insert your view controller class name here
{
// specify what kind of Orientation you want
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
}
}
// show every other View Controller in portrait
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.