【问题标题】:iOS7 - Lock Orientation of only SOME View Controllers that are within a Tab Bar?iOS7 - 仅锁定选项卡栏中的某些视图控制器的方向?
【发布时间】:2014-01-08 08:02:51
【问题描述】:

我知道这个问题已经被问过无数次了,虽然我根本无法让它工作,实际上没有任何答案有效,也许我没有正确复制代码,但无论哪种方式它不会工作。

无论如何,我的应用程序的视图控制器布局如下,注意这是一个选项卡式应用程序,而不是单个视图应用程序:

主标签栏控制器 > 分支成多个导航栏控制器(都引用同一个 CustomNavigationController 类) > 每个导航控制器下都有许多 UIViewController,其中一些引用自定义视图控制器类。 (有关层次结构的可视化表示,请参见图片)

我的问题是:我怎样才能将我的应用程序中所有页面的方向锁定为仅纵向模式,但是允许一些页面(UIViewControllers)能够旋转如果用户旋转设备,横向? (上图中标有星号的页面是我希望允许旋转的页面)

记住我想要允许旋转的视图控制器是导航控制器的子级,导航控制器也是标签栏控制器的子级。另外,请注意其中一个 View 控制器不在 Navigation 控制器下,它位于 Tab Bar 控制器下,但不确定这会产生多大影响。

非常感谢

我的应用相关类的代码

选项卡栏控制器代码(注意这里没有有意义的代码,因为我以前从来没有为此设置过类)

@interface TabBarController ()

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}  
@end

导航控制器类(也从来没有这个类

@interface NavigationBar ()
@end   
@implementation NavigationBar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"NavigationController";
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

首页类(必须纵向锁定)

@interface HomePage ()

@end

@implementation HomePage


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"HomePage";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // If the condition is not true/not YES, than create and show alert.
    if (![[NSUserDefaults standardUserDefaults]
          boolForKey:@"didShowOneTimeAlert"]) {

        //Define and Edit the Alert details.
        UIAlertView *zoomTip = [[UIAlertView alloc] initWithTitle:@"Tips" message:@"On all web site and map pages, pinch in and out to zoom. \n \n On the 'Program' page, tap on a session to view more details." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];

        //Show the alert
        [zoomTip show];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"didShowOneTimeAlert"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    } // End if
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end

地图类(显示地图且必须能够旋转的页面)

@interface MezzanineLevelMap ()
@end
@implementation MezzanineLevelMap
@synthesize scrollView, imageView;


-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Mezzanine Level"]];
    self.imageView = tempImageView;

    scrollView.maximumZoomScale = 1;
    scrollView.minimumZoomScale = .25;
    scrollView.clipsToBounds = YES;
    scrollView.delegate = self;
    [scrollView addSubview:imageView];
    scrollView.zoomScale = .25;

    // Change scroll view sizes according to the screen size
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

        CGSize result = [[UIScreen mainScreen] bounds].size;
        if (result.height == 480) {
            [scrollView setFrame:CGRectMake(0, 5, 320, 475)];
        } else {
            [scrollView setFrame:CGRectMake(0, 5, 320, 563)];
        } // End if
    } // End if
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

    标签: ios iphone xcode ios7 orientation


    【解决方案1】:

    只需在您的视图控制器(iOS 6+)中实现以下内容:

    - (BOOL)shouldAutorotate {
       return NO;
    }
    

    【讨论】:

      【解决方案2】:

      在每个视图控制器中,您可以编写以下代码并根据要求返回是或否。

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
          return YES/NO;
      }
      

      【讨论】:

      • 也许我没有正确实现它,但我所做的只是将您的代码复制到我创建的 UIViewController 类中,该类引用了视图控制器,然后我返回了“否”。这没有锁定该视图控制器的方向,我仍然能够旋转设备方向。然后我尝试返回“是”,这也没有做任何事情。有什么想法吗?
      • 请参考此网址一次:stackoverflow.com/questions/14174449/…
      • 您能否解释一下我应该如何使用该线程提供的答案中的代码?当我将它复制到我的标签栏控制器时,什么也没发生,当复制到视图控制器时它返回错误。
      • 请试试这个方法 (stackoverflow.com/questions/12630359/… (BOOL)shouldAutorotate { return YES; }
      • 您能不能说得更具体一点?我已经尝试过该页面上的代码,但它不起作用,我真的觉得好像我没有正确实现它。如果你能告诉我在哪里放什么代码,那将不胜感激。请记住,我想允许旋转的视图控制器位于标签栏控制器下的导航控制器下,我觉得这是我的问题的很大一部分。谢谢大家
      【解决方案3】:

      解决这个问题的很好的描述是 http://koreyhinton.com/blog/lock-screen-rotation-in-ios8.html

      【讨论】:

      • 欢迎来到 Stackoverflow。请不要只是将链接粘贴为答案,因为链接可能会随着时间而改变,请提供答案本身或至少提供链接的更多上下文。见stackoverflow.com/help/how-to-answer
      猜你喜欢
      • 2011-12-17
      • 2017-10-03
      • 1970-01-01
      • 2016-08-18
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      相关资源
      最近更新 更多