我可以手动触发viewController上的supportedInterfaceOrientations吗?

时间:2014-04-16 13:49:26

标签: cocoa-touch uinavigationcontroller screen-rotation

我的应用程序中有一个UINavigationController子类作为根viewController。我将各种其他视图控制器推送到导航控制器的viewControllers堆栈。

其中一些允许以任何方向显示,有些只是风景。

我已使用此链接中的信息:http://grokin.gs/blog/2012/11/16/orientationrespectfulnavigationcontroller/ 因此,当设备旋转发生时,导航控制器会正确报告顶视图控制器的允许方向。但是,当新的视图控制器被推入堆栈时,我似乎无法解决如何触发自动旋转。

e.g。应用程序的根vc是我的naviationViewController。它的初始顶级vc可以横向或纵向显示。假设设备是纵向的,现在我将另一个vc推到导航控制器上。这个新的vc只能以横向显示。如果设备旋转360度,则会发生正确的事情,即navController将旋转限制为横向。但是,如果设备从未移动过,则在navController上不会调用supportedInterfaceOrientations :,因此设备不知道应该自动将UI旋转为横向。

那么 - 我可以触发UIApplication做旋转发生时做的事吗?或者我错过了什么?

感谢。

2 个答案:

答案 0 :(得分:1)

因此,您似乎可以触发系统重新评估允许哪些设备方向。只要viewController以模式全屏显示,就会发生这种情况。

因此,(这是一个讨厌的黑客,但似乎有效)你可以要求根VC呈现一个只允许横​​向方向的VC,然后一旦它被呈现,再次将其解除。

e.g。见下面的代码:

- (void)viewDidAppear:(BOOL)animated
{
    //  Force an orientation update if we are not in landscape...

    BOOL force = YES;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIDeviceOrientationLandscapeRight || orientation == UIDeviceOrientationLandscapeLeft)
    {
        force = NO;
    }

    if (force)
    {
        PortraitOnlyViewController *newVC = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"forceLandscape"];

        [self.navigationController presentViewController:newVC animated:NO completion:^(void){
            NSLog(@"forcing VC has been presented.");
            [self.navigationController dismissViewControllerAnimated:NO completion:^(void){
                NSLog(@"forcing VC has been dismissed");
            }];
        }];

    }

}

答案 1 :(得分:1)

我不得不修改Diggory的答案,因为我没有使用UINavigationController,也使用Swift。我在iOS 8.3上也遇到以下错误:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(environment.ServiceUrl);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    HttpResponseMessage response = client.DeleteAsync("api/Producer/" + _nopProducerId).Result;
    if (response.IsSuccessStatusCode)
    {
        string strResult = response.Content.ReadAsAsync<string>().Result;
    }
}

通过添加对dispatch_get_main_queue的调用解决了这个问题。

Trying to dismiss the presentation controller while transitioning already. (<_UIFullscreenPresentationController