简体   繁体   English

iOS:UINavigationController中的锁定方向

[英]IOS: lock orientation in a UINavigationController

In my app I start the secondviewcontroller with a navigation controller in portrait orientation, and I finish in a fourthviewcontroller where I should rotate the iPhone to show an image. 在我的应用中,我使用纵向导航控制器启动了第二视图控制器,然后在第四视图控制器中完成了操作,在该控制器中,我应该旋转iPhone以显示图像。 The problem now is: if in the target I set the orientation in this way: 现在的问题是:如果在目标中以这种方式设置方向: 在此处输入图片说明

I can use this method to rotate my image: 我可以使用此方法旋转图像:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

and it work fine, but I can't lock all view that belong at navigationcontroller, because all them rotate il portrait and landscape. 并且它工作正常,但我无法锁定所有属于navigationcontroller的视图,因为它们全部旋转il纵向和横向。

the second option is to set the target in this way: 第二种选择是以这种方式设置目标: 在此处输入图片说明

but this structure don't detect the orientation: 但是此结构无法检测方向:

in view will appear: 在视图中将出现:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    [self willRotateToInterfaceOrientation:orientation duration:1];

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

- (BOOL)shouldAutorotate
{
        return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscape;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {

        }
        else
        {

        }
    }

    else{

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        {

        }
        else
        {

        }

    }
}

what's the best solution? 最好的解决方案是什么? thanks 谢谢

I can't help but feel that if I had read the docs it would have saved me some time, but for me the solution was to subclass UINavigaionController and add the following: 我禁不住觉得,如果我阅读了文档,那可以节省一些时间,但是对我来说,解决方案是将UINavigaionController 子类化并添加以下内容:

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

And since there really isn't anything substantial going on here, there's no need to override init with coder or anything else. 而且,由于这里实际上没有发生任何实质性的事情,因此无需使用编码器或其他任何方法覆盖init。 You can just reference this subclass everywhere you previously referenced your navigation controller, including Interface Builder. 您可以在以前引用导航控制器(包括Interface Builder)的任何地方引用该子类。

Go with the first conifguration. 进行第一个配置。 And implement the method 并实施方法

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

for all view controllers. 适用于所有视图控制器。 Allow landscape only for the wanted viewcontroller. 只允许所需的视图控制器使用横向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM