如何在 iOS 8 中锁定 Objective-C iPhone 应用程序特定视图的方向?
How can I lock orientation for a specific view an Objective-C iPhone app in iOS 8?
我只是想设置我的应用程序,以便在横向模式下只能查看一个视图。
我几乎尝试了从 shouldAutorotate
到 supportedInterfaceOrientation
再到 preferedInterfaceOrientationForPresentation
的所有方法,并将 [UIDevice currentDevice]
的方向设置为纵向。我在 Info.plist 和项目的常规部分启用了景观。
在您的应用委托中,您可以这样做:
// 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)
}
首先把你的申请头像只放在你的info.plist
在您的 AppDelegate class 中创建以下 属性:
@property BOOL restrictRotation;
然后在你的 AppDelegate.m class 中创建以下方法:
-(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 及更高版本,要在使用 答案时清除 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;
}
我只是想设置我的应用程序,以便在横向模式下只能查看一个视图。
我几乎尝试了从 shouldAutorotate
到 supportedInterfaceOrientation
再到 preferedInterfaceOrientationForPresentation
的所有方法,并将 [UIDevice currentDevice]
的方向设置为纵向。我在 Info.plist 和项目的常规部分启用了景观。
在您的应用委托中,您可以这样做:
// 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)
}
首先把你的申请头像只放在你的info.plist
在您的 AppDelegate class 中创建以下 属性:
@property BOOL restrictRotation;
然后在你的 AppDelegate.m class 中创建以下方法:
-(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 及更高版本,要在使用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window API_AVAILABLE(ios(6.0)) API_UNAVAILABLE(tvos)
{
if(self.restrictRotation)
return UIInterfaceOrientationMaskPortrait;
else
return UIInterfaceOrientationMaskAll;
}