加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Objective-C中iOS 6上的强制横向方向

发布时间:2020-12-15 01:44:22 所属栏目:百科 来源:网络整理
导读:我有一个UINavigationController内的主视图控制器.在那个主视图控制器中,我有一个按钮,它推动一个内部有UIWebView的详细视图控制器.我希望这个细节视图控制器在加载时处于横向模式.回到主视图控制器,它再次强制返回到纵向模式.我正在运行iOS 6. 我已经看到了
我有一个UINavigationController内的主视图控制器.在那个主视图控制器中,我有一个按钮,它推动一个内部有UIWebView的详细视图控制器.我希望这个细节视图控制器在加载时处于横向模式.回到主视图控制器,它再次强制返回到纵向模式.我正在运行iOS 6.

我已经看到了其他类似的问题,但它并不适用于我的目的.我创建了一个LandscapeViewController,它是UIViewController的子类,我在这里编写了这些方法:

#pragma mark - Orientation Methods
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

当我按下详细视图控制器时,这是我的代码:

DetailViewController *detailVC = [[DetailViewController alloc]
                                      initWithNibName:@"DetailViewController"
                                      bundle:nil];

    [self.navigationController pushViewController:detailVC
                                         animated:YES];

我正在考虑在上面的代码中将我的LandscapeViewController子类化到哪里以使其工作或如何正确地子类化并推送我的详细视图控制器.如果导航控制器无法将我的细节视图控制器从纵向推送到横向,我也可以以模态方式呈现我的详细视图控制器.我在哪里做错了?

解决方法

考虑到:
视图A:仅限纵向 – 视图B:仅限横向

我无法在导航控制器中执行此操作.相反,我所做的是从视图A打开模态视图以查看B并强制新的导航控制器进入此视图.

这在iOS5中对我有用.

您需要为导航控制器创建一个类别,如下所示:

UINavigationController Rotation_IOS6.h

#import <UIKit/UIKit.h> 

@interface UINavigationController (Rotation_IOS6)

- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;

@end

UINavigationController Rotation_IOS6.h

#import "UINavigationController+Rotation_IOS6.h"

@implementation UINavigationController (Rotation_IOS6)

- (BOOL)shouldAutorotate
{
    return [self.topViewController shouldAutorotate];
}

- (NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [self.topViewController preferredInterfaceOrientationForPresentation];
}

@end

在AppDelegate.m中添加:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskAll;
}

然后在视图A中:

- (BOOL)shouldAutorotate {
    return YES;
}

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

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

同样在View A中打开View B执行此操作:

ViewB *vc = [[ViewB alloc] initWithNibName:@"ViewB" bundle:nil];

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:vc];

[self presentViewController:navigationController animated:YES completion:nil];

最后,在View B中

- (BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读