首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在导航到UIViewController时将其旋转到其支持的界面方向?

如何在导航到UIViewController时将其旋转到其支持的界面方向?
EN

Stack Overflow用户
提问于 2016-01-07 20:38:18
回答 2查看 542关注 0票数 2

我有一个视图控制器(带有子视图控制器),它被锁定在纵向,使用以下代码来完成此操作:

代码语言:javascript
运行
复制
- (UIInterfaceOrientationMask) supportedInterfaceOrientations {
    if ([self.centerViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
        return [self.centerViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskAll;
}

这非常有效,如果需要锁定视图,我可以“覆盖”当前centerViewController中的supportedInterfaceOrientations,如果希望视图支持所有内容,则可以省略它。

问题是,被锁定的视图在导航时不会旋转到其支持的方向。一个视图锁定在纵向视图中,但当在横向视图中显示另一个视图并导航到此视图时,即使此视图不支持横向视图,也会在横向视图中显示该视图。

如何确保视图在导航时旋转到允许的方向?

EN

回答 2

Stack Overflow用户

发布于 2016-01-07 20:43:07

如果您在supportedInterfaceOrientations中返回了受支持的方向,我将自动旋转。

代码语言:javascript
运行
复制
#pragma mark - Orientation Handling
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)shouldAutorotate {// iOS 6 autorotation fix
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {// iOS 6 autorotation fix
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {// iOS 6 autorotation fix
    return UIInterfaceOrientationPortrait;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
    }
}
票数 0
EN

Stack Overflow用户

发布于 2016-01-07 20:58:08

AppDelegate.h中添加以下属性

代码语言:javascript
运行
复制
@property (readwrite) BOOL restrictRotation;

AppDelegate.m中添加以下代码

代码语言:javascript
运行
复制
-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    if(self.restrictRotation)
        return UIInterfaceOrientationMaskLandscape;
    else
        return UIInterfaceOrientationMaskPortrait;
}

在要限制方向的ViewController.m中添加以下代码:

代码语言:javascript
运行
复制
-(BOOL)shouldAutorotate
{
    return NO;
}

-(void) restrictRotation:(BOOL) restriction
{
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    appDelegate.restrictRotation = restriction;
}

- (NSUInteger) supportedInterfaceOrientations {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskLandscape;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    //  (iOS 6)
    //  Prefer (force) landscape
    return UIInterfaceOrientationLandscapeRight;
}

同样,当你离开你的Landscape ViewController.m时,进行下面的函数调用

代码语言:javascript
运行
复制
[self restrictRotation:NO];

希望我能解决你的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34655338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档