簡體   English   中英

使用UITabbar的UINavigationController中的方向鎖定

[英]Orientation lock in UINavigationController with UITabbar

我想在幾個UIViewcontroller中鎖定方向

1)Loginview(僅支持縱向)具有登錄按鈕的UIViewcontroller

2)在登錄按鈕上,我從情節提要中調用UITabbarController,具有3個選項卡,所有選項卡都嵌入了UINavigationControllers。(Tab1,Tab2,Tab3)

3)在Tab1(同時支持橫向和縱向)上具有按鈕button1,我從中按下一個UIViewcontroller Demo(僅支持縱向)。 像下面

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

該演示已成功加載,但我想將其旋轉鎖定為縱向,因此我創建了UINavigationController的Category,但它仍在旋轉。

#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

// -------在我的Demo uiviewcontroller i內部,但在代碼下面,但它從未調用過--------

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

我讀了很多帖子,但沒有一個可以提供幫助,請幫助我,我該怎么做?

初始設置方向為僅在目標->常規->部署信息中為縱向 (請參見下圖)

在此處輸入圖片說明

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的Implemenatation中,應該將setRotate值設置為:

如果要旋轉,請設置為

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

否則設置NO

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

注意: setShouldRotate:始終在viewDidAppear:中調用。 因此,每當您切換UIViewController時,它將正常工作。

希望這對您有用。 祝一切順利 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM