首页 文章

自定义视图控制器演示导致导航栏反弹

提问于
浏览
1

我正在使用从根视图控制器模态转换委托来呈现视图控制器 .

UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
UIViewController *rootVC = [window rootViewController];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:authVC];
navController.modalPresentationStyle = UIModalPresentationCustom;
navController.navigationBar.translucent = NO;
navController.transitioningDelegate = self;
[rootVC presentViewController:navController animated:YES completion:nil];

我的转换委托添加了如下视图,其中 authorizationVC 是截图中描绘的登录视图 .

UIView *containerView = [transitionContext containerView];
[containerView addSubview:blurredView];
[containerView insertSubview:_authorizationVC.view aboveSubview:blurredView];

_authorizationVC.view.frame = CGRectMake(10, 30, 300, 450);

首先,视图设置为动画,导航栏为全高,我认为是64像素(导航栏为44,状态栏为20) .

Full height nav bar

动画完成后,导航栏会缩小到44像素 . 转变是不和谐的 . 我的视图控制器内的内容不受影响 .

Nav bar with correct height (44 pixels)

如何避免这种抖动的导航栏?第二个图像是我想要实现的 .

2 回答

  • 2

    在将视图添加到其superview之前设置视图的所有属性 .

    UIView *containerView = [transitionContext containerView];
    _authorizationVC.view.frame = CGRectMake(10, 30, 300, 450); /// Change the frame FIRST!
    
    [containerView addSubview:blurredView];
    [containerView insertSubview:_authorizationVC.view aboveSubview:blurredView];
    

    瞧!导航栏按预期运行 .

  • 1

    如果你真的不需要它,我会避免使用 UINavigationController .

    UINavigationController 在调整导航栏大小的方式中考虑了 topLayoutGuide . 如果您只想要彩色条和关闭按钮,我会简化它并使用您自己的视图 .

    如果你必须使用 UINavigationController ,你可以尝试使用状态栏's appearance and see how it affects the navigation controller'的演示文稿 .

相关问题