【问题标题】:iOS opaque UIViewController on top of UINavigationControllerUINavigationController 之上的 iOS 不透明 UIViewController
【发布时间】:2014-06-09 16:04:09
【问题描述】:

我正在尝试从 UINavigationController 呈现不透明的 UIViewController。当视图被动画化时,它是不透明的,就像我想要的那样。然而,当它完成动画时,背景会变成灰色。当前一个视图是 UIViewController 时,我有这个工作。当我使用 UINavigationController 作为上一个视图时,我开始遇到这个问题。

切换到视图的代码:

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:STORYBOARD_IPHONE bundle:nil];
UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:viewName];
[self presentViewController:viewController animated:YES completion:nil];

使呈现的视图不透明的代码:

self.view.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.7];

我认为这可能与呈现视图的类不是 UINavigationController,而是 UINavigationController 中的 UIViewController 这一事实有关。所以我尝试使用 self.navigationController presentViewController,但我得到了相同的结果。

如何解决这个问题,使背景保持不透明?

上一个带有嵌入式 UIViewController 的 UINavigationController:

动画时的样子:

动画后的样子:

【问题讨论】:

  • 您在哪里编写背景颜色代码。它在 viewdidload 中吗?如果是,则尝试在 viewwillappear 中设置背景色。
  • 是的,我是在 viewDidLoad 中设置的。我尝试在 viewWillAppear 和 viewDidAppear 中设置它。我也有同样的问题。
  • 您将视图设置为 alpha 0.7 并想知道为什么可以看穿它?你的观点背后是什么——可能是黑色背景?那和 70% 的白色可能导致你的灰色。
  • @OwenHartnett 一定是黑色背景,但是为什么是黑色背景呢?为什么它后面的 UINavigationController 不像是 UIViewController 时那样显示?
  • UINavigationController 是否有一个背后有白色背景的视图?或者那个区域是透明的,你正在看它后面的 UIwindow,它可能有一个黑色的背景?你是在设置 rootviewController,还是只是在 UINavigationController 中调用 init?

标签: ios uiviewcontroller uinavigationcontroller


【解决方案1】:

这是我经常用来查看哪些视图位于我感兴趣的视图后面的例程:

// useful debugging method - send it a view and it will log all subviews
// can be called from the debugger
- (void) viewAllSubviews:(UIView *) topView Indent:(NSString *) indent  {
    for (UIView * theView in [topView subviews]){
        NSLog(@"%@%@", indent, theView);
        if ([theView subviews] != nil)
            [self viewAllSubviews:theView Indent: [NSString stringWithFormat:@"%@ ",indent]];
    }
}

我通常把它放在可以全局访问它的地方。像这样称呼它

[self viewAllSubviews:rootViewController.view Indent:@"  "];

它会在控制台上打印以你给它的任何视图为根的视图树,并根据级别缩进子视图。它应该显示您感兴趣的对象背后的内容,并且您可以检查那里每个视图的框架坐标。

这应该可以很好地帮助您了解背后的视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多