【问题标题】:IOS: lock orientation in a UINavigationControllerIOS:在 UINavigationController 中锁定方向
【发布时间】:2013-08-20 12:13:58
【问题描述】:

在我的应用程序中,我使用纵向导航控制器启动 secondviewcontroller,然后在第四个viewcontroller 中完成,我应该在其中旋转 iPhone 以显示图像。 现在的问题是: 如果在目标中我以这种方式设置方向:

我可以使用这个方法来旋转我的图像:

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

它工作正常,但我无法锁定属于导航控制器的所有视图,因为它们都可以旋转纵向和横向。

第二个选项是以这种方式设置目标:

但是这个结构没有检测到方向:

会出现在视图中:

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
        {

        }

    }
}

什么是最好的解决方案? 谢谢

【问题讨论】:

    标签: ios orientation landscape-portrait


    【解决方案1】:

    我不禁觉得,如果我阅读了文档,它会为我节省一些时间,但对我来说,解决方案是 subclass UINavigaionController 并添加以下内容:

    - (BOOL)shouldAutorotate
    {
        return NO;
    }
    
    - (NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return UIInterfaceOrientationPortrait;
    }
    

    而且由于这里实际上并没有发生任何实质性的事情,因此无需使用 coder 或其他任何东西来覆盖 init。您可以在之前引用导航控制器的任何地方引用这个子类,包括 Interface Builder。

    【讨论】:

    • 我在其他视图控制器中添加了此代码,我想锁定但方向处于活动状态...我该怎么办?
    【解决方案2】:

    使用第一个配置。并实现方法

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

    适用于所有视图控制器。仅允许所需视图控制器的横向。

    【讨论】:

    • 不,这不可能...第一个配置的轮换是自动的
    猜你喜欢
    • 2017-08-22
    • 1970-01-01
    • 2021-09-17
    • 2016-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多