当导航到UIViewController时,如何将UIViewController旋转到支持的界面方向? [英] How can I rotate a UIViewController to its supported interface orientation when navigating to it?

查看:114
本文介绍了当导航到UIViewController时,如何将UIViewController旋转到支持的界面方向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我有一个viewcontroller(有子viewControllers)被锁定在纵向,使用下面的代码来实现这一点: - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if([self.centerViewController responsesToSelector:@selector(supportedInterfaceOrientations)]){
return [self.centerViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskAll;
}

这完美地工作,我可以覆盖supportedInterfaceOrientations在我当前的centerViewController if我需要锁定该视图,如果我想让视图支持所有的东西,就离开它。



问题是被锁定的视图不会旋转支持导航导航。一个视图被锁定在纵向,但是当在横向显示另一个视图并导航到这个视图显示在横向,即使风景不支持此视图。



如何 c>在中添加以下属性 AppDelegate.h

  @property(readwrite)BOOL restrictRotation; 

AppDelegate.m / p>

   - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{
如果(self.restrictRotation)
return UIInterfaceOrientationMaskLandscape;
else
return UIInterfaceOrientationMaskPortrait;
}

在ViewController.m中添加要限制方向的下列代码: p>

   - (BOOL)shouldAutorotate 
{
return NO;
}

- (void)restrictRotation:(BOOL)限制
{
AppDelegate * appDelegate =(AppDelegate *)[UIApplication sharedApplication] .delegate;
appDelegate.restrictRotation = restriction;
}

- (NSUInteger)supportedInterfaceOrientations {
//返回支持的方向的位掩码。如果你需要更多,
//使用按位或(见注释的返回)。
return UIInterfaceOrientationMaskLandscape;
// return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
//(iOS 6)
//首选(强制)横向
return UIInterfaceOrientationLandscapeRight;
}

当你离开Landscape ViewController.m时, p>

  [self restrictRotation:NO];希望我解决你的问题。

I have a viewcontroller (with child viewcontrollers) that is locked in portrait, using the following code to accomplish this:

- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
    if ([self.centerViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.centerViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskAll;
}

This works perfectly, I can "override" the supportedInterfaceOrientations in my current centerViewController if I need to lock that view, and leave it out if I want the view to support everything.

The problem is that the views that are locked don't rotate to their supported orientation upon navigation. One view is locked in portrait, but when showing another view in landscape and navigating to this one shows it in landscape, even though landscape is not supported for this view.

How can I make sure the view is rotated to an allowed orientation upon navigating?

解决方案

Add following property in AppDelegate.h

@property (readwrite) BOOL restrictRotation;

Add following code in AppDelegate.m

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.restrictRotation)
        return UIInterfaceOrientationMaskLandscape;
    else
        return UIInterfaceOrientationMaskPortrait;
}

Add following code in ViewController.m which you want to restrict orientation:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
}

- (NSUInteger) supportedInterfaceOrientations {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskLandscape;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //  (iOS 6)
    //  Prefer (force) landscape
    return UIInterfaceOrientationLandscapeRight;
}

Also when you leaving from your Landscape ViewController.m make following function call

[self restrictRotation:NO];

Hopefully i solves your problem.

这篇关于当导航到UIViewController时,如何将UIViewController旋转到支持的界面方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆