【问题标题】:Force Landscape view in IOS6IOS6中的强制横向视图
【发布时间】:2013-01-05 22:28:17
【问题描述】:

我知道有很多关于如何在 IOS6 中强制定向的线程,但它们似乎都不适合我,所以现在我需要一些帮助来解决这个问题。

我有一个基于导航的应用程序,它有许多视图控制器。除了必须以横向模式加载(无需用户先转动手机)之外,所有这些都是纵向视图。

在我的导航控制器的实现中,我添加了 shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOriantationForPresentation。

@implementation UINavigationController (Rotation_IOS6)

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

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

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

@end

所以它返回在每个视图控制器中定义的值。

在应用委托中,我有supportedInterfaceOrientationsForWindow。

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{

//Default orientations value
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;

//Get orientation from the last view controller
if(self.window.rootViewController){
    UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
    orientations = [presentedViewController supportedInterfaceOrientations];
}

return orientations;
}

在每个视图控制器中,我都有该视图的设置,例如:

// Only allow portrait view
-(NSInteger)supportedInterfaceOrientations{    
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate {
    return YES;
}

然后我像这样推送下一个视图控制器:

NextViewController *nxtController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nxtController animated:YES];

但是当我推动横向视图控制器时,在纵向握住手机的同时,它也会以纵向模式加载。如果我然后倾斜手机,它会触发自动旋转功能并将视图旋转到横向模式,然后将其锁定在该模式下。但是,我需要将其锁定为横向模式,而不使用手机方向来触发它来检查自动旋转。 有什么想法吗?

【问题讨论】:

标签: ios uiviewcontroller ios6 uinavigationcontroller landscape


【解决方案1】:

您可以尝试通过使用总是返回横向的 shouldaAutorotateToInterfaceOrientation 来强制 viewController 以横向显示,例如:

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

【讨论】:

    【解决方案2】:

    转到项目设置并删除您不想要的模式上的选择。

    【讨论】:

      猜你喜欢
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多