iOS 8:以纵向方式呈现模态视图控制器会导致底层横向导航控制器的导航栏调整大小 [英] iOS 8: Presenting a modal view controller in portrait causes resize of the navigation bar of the underlying landscape navigation controller

查看:165
本文介绍了iOS 8:以纵向方式呈现模态视图控制器会导致底层横向导航控制器的导航栏调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 8上,我对导航栏和方向更改有一个奇怪的行为。

On iOS 8 I have a strange behavior regarding the navigation bar and orientation changes.

我有一个导航控制器,报告支持的界面方向 UIInterfaceOrientationMaskLandscapeRight 。导航栏具有预期的横向高度(遗憾的是我无权发布截图)。

I have a navigation controller which reports a supported interface orientation UIInterfaceOrientationMaskLandscapeRight. The navigation bar has the expected height for landscape orientation (sadly I am not entitled to post screenshots).

然后我启动一个仅支持<的视图控制器的模态演示code> UIInterfaceOrientationMaskPortrait 。当演示动画开始时,似乎底层导航控制器的指标被更改为纵向演示,因为导航栏的高度增长到其纵向大小,如上所示。

Then I initiate a modal presentation of a view controller that only supports UIInterfaceOrientationMaskPortrait. When the presentation animation starts, it seems that the metrics of the underlying navigation controller are changed to a portrait presentation, as the height of the navigation bar grows to its portrait size, as depicted above.

iOS 7不会出现此行为。我错过了什么?我想恢复旧的行为。

iOS 7 does not exhibit this behavior. What am I missing? I want to restore the old behavior.

以下是上述简单示例的完整代码:

Here is the full code of the simple example above:

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];


    DOGButtonViewController *root = [DOGButtonViewController new];
    DOGOrientedNavigationController *navi = [[DOGOrientedNavigationController alloc] initWithRootViewController:root];
    navi.allowedInterfaceOrientations = UIInterfaceOrientationMaskLandscapeRight;

    self.window.rootViewController = navi;

    [self.window makeKeyAndVisible];
    return YES;
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait;
}

@end


@implementation DOGOrientedNavigationController

- (NSUInteger)supportedInterfaceOrientations
{
    return self.allowedInterfaceOrientations;
}

@end

@implementation DOGButtonViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Button View Controller";
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (IBAction)buttonClicked:(id)sender
{
    DOGPortraitViewController *vc = [DOGPortraitViewController new];
    [self presentViewController:vc animated:YES completion:nil];
}

@end

@implementation DOGPortraitViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Portrait Title";
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (IBAction)buttonClicked:(id)sender
{
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end

在更复杂的设置中我还可以在呈现纵向模态时按比例放大导航控制器中包含的UIWebView中的文本。当解雇模态时,文本不会调整为原始大小。

In a more complex setup I also experience the text in a UIWebView contained in the navigation controller being scaled up when presenting the portrait modal. When dismissing the modal, the text is not resized to its original size.

推荐答案

由于缺乏更好的选择,我已经完成了对此有点骇人听闻。
基本上在我显示模态视图之前,我拍摄一个屏幕截图并将其放在呈现视图控制器的顶部。

For lack of a better option I've done a bit of a hack for this. Basically before I show the modal view I take a screen shot and lay it on top of the presenting view controller.

显然我必须删除此屏幕截图当视图重新出现时

Obviously I have to remove this screen shot when the view re-appears

  func showScreenShot () {
    let image = screenShot()
    self.screenShotImageView = UIImageView(image: image)
    self.view.addSubview(self.screenShotImageView!)
  }

func screenShot () -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, true, UIScreen.mainScreen().scale)
    self.view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image
}

func removeScreenShot () {
  if let screenImageView = self.screenShotImageView {
   screenImageView.removeFromSuperview()
   self.screenShotImageView = nil
  }
}

这篇关于iOS 8:以纵向方式呈现模态视图控制器会导致底层横向导航控制器的导航栏调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆