xcode - 如何将视图锁定为纵向模式,但仍允许一个视图旋转?

时间:2013-02-27 11:22:04

标签: iphone xcode orientation portrait screen-rotation

我遇到设备轮换问题。除了一个视图,我显示公司启动画面,我想将所有剩余的应用程序视图锁定到纵向显示。在项目设置中,支持的方向是Portrait和LandscapeLeft。在“公司飞溅”中它工作正常,无论我如何旋转设备,视图旋转都锁定在LandscapeLeft中。在我将设备向左旋转的所有其他视图中,视图会改变而不是保持纵向显示。这些方法甚至没有开火?如果我从项目中支持的方向中删除横向,则会拧紧“公司启动”视图。我尝试将shouldAutorotate返回更改为NO,但这没有用。试图通过here发布的建议,但这没有帮助。如果我将以下代码放入我的AppDelegate.m,一切都被锁定为纵向模式,并且“公司启动”在访问时崩溃。

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

除了一个屏幕外,无论设备如何旋转,如何将视图锁定为纵向模式?

来自'Company Splash'视图的

**方法。再次,应该像它一样工作。

-(NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft;
}

**来自所有其他视图的方法,当我不想要它们时,它们会从画像中旋转

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // IOS5 Only returning that it should rotate to potrait
    return (interfaceOrientation == UIDeviceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    // forcing the rotate IOS6 Only
    return YES;
}

-(NSInteger)supportedInterfaceOrientations
{
    // return number or enum IOS6 Only
    return UIInterfaceOrientationMaskPortrait;
}

我想也许可能是因为UITabBarController是根控制器而我在ViewController中呢?这些方法甚至没有开火?

3 个答案:

答案 0 :(得分:6)

将观察者添加到要旋转的视图的viewDidLoad方法,如下所示:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
 addObserver:self selector:@selector(orientationChanged:)
 name:UIDeviceOrientationDidChangeNotification
 object:[UIDevice currentDevice]];

然后根据 orientationChanged 方法中的横向视图设置视图,如下所示:

- (void) orientationChanged:(NSNotification *)note{
UIDevice * device = [UIDevice currentDevice];
switch(device.orientation)
{
    case UIDeviceOrientationPortrait:

        break;
    case UIDeviceOrientationPortraitUpsideDown:

        break;
    case UIDeviceOrientationLandscapeLeft:

        break;
    case UIDeviceOrientationLandscapeRight:

        break;

    default:
        break;
  };
}

答案 1 :(得分:2)

将此方法拖放到要以某个方向锁定的视图控制器中。

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

只需更改到您想要的方向(上面将其锁定在横向中)

答案 2 :(得分:0)

也许你应该允许所有的方向,并锁定每个班级的纵向,除了公司飞溅

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