【问题标题】:Orientation lock in UINavigationController with UITabbarUINavigationController 中的方向锁定与 UITabbar
【发布时间】:2017-08-22 06:30:19
【问题描述】:

我想在几个 UIViewcontroller 中锁定方向

1) 登录视图(仅支持纵向) UIViewcontroller 具有登录按钮

2)在登录按钮上,我从情节提要中调用 UITabbarController,有 3 个选项卡,所有选项卡都嵌入 UINavigationControllers。(Tab1,Tab2,Tab3)

3) 在 Tab1 上(支持横向和纵向)具有按钮 button1,我从中推送 UIViewcontroller 演示(仅支持纵向)。如下所示

 Demo *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"Demo"];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

Demo 已成功加载,但我想将其旋转锁定为纵向,因此我创建了 UINavigationController 类别,但它仍在旋转。

#import <UIKit/UIKit.h>

@interface UINavigationController (Orientation)

@end

//==========

#import "UINavigationController+Orientation.h"
@implementation UINavigationController (Orientation)
-(BOOL)shouldAutorotate
{

    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortraitUpsideDown;;
}
@end

//------- 在我的演示 uiviewcontroller 中,但在代码下方,但它从未调用过--------

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

我看了很多帖子,但都没有帮助,请帮我看看我该怎么做?

【问题讨论】:

    标签: ios objective-c iphone uiviewcontroller uiinterfaceorientation


    【解决方案1】:

    仅在 Targets -> General -> Deployment Info 中将初始设置方向设置为 Portrait(参见下图)

    AppDelegate.h中声明属性:

    @property (assign, nonatomic) BOOL shouldRotate;
    

    AppDelegate.m 中定义

    -(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        if (self.shouldRotate)
            return UIInterfaceOrientationMaskAllButUpsideDown;
        else
            return UIInterfaceOrientationMaskPortrait;
    }
    

    UIViewController 实现 中设置 shouldRotate 值,如:

    如果要旋转,则设置为 YES

    -(void) viewDidAppear:(BOOL)animated {
            [super viewDidAppear:animated];
            [(AppDelegate *)[[UIApplication sharedApplication] delegate] setShouldRotate:YES];
        }
    

    否则设置否

    -(void) viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        [(AppDelegate *)[[UIApplication sharedApplication] delegate] setShouldRotate:NO];
    }
    

    注意: setShouldRotate: 总是调用 viewDidAppear:。所以,每当你切换 UIViewController 时,它都会正常工作。

    希望这对你有用。一切顺利:)

    【讨论】:

    • 感谢 yeshica...寻求帮助...但如果用户处于横向模式,则跳转视图也显示为横向...。
    • @iphonemaclover: setShouldRotate 在那个 ViewController `viewDidAppear' 方法中的值为 NO
    • @iphonemaclover:如果您发现此代码有帮助,请不要仅仅感谢标记已接受。
    • 已经投票支持@Yashica ....但接受的答案应该处于工作状态...所以它将解决我的问题并帮助其他用户...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    相关资源
    最近更新 更多