objective-c 升级到 Xcode 5.1 和 iOS 7.1 后,segue 转换期间导航栏上的暗影

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22413193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:12:39  来源:igfitidea点击:

Dark shadow on navigation bar during segue transition after upgrading to Xcode 5.1 and iOS 7.1

objective-cuinavigationcontrolleruinavigationbarsegue

提问by Nihat

When I am navigating back & forth between parent and child controllers in a master - detail navigation controller, i see a dark shadow on the right side of navigation bar at top. It started after I upgraded to Xcode 5.1. It feels rough and distracting. How can I get rid of it?

当我在主 - 细节导航控制器中的父控制器和子控制器之间来回导航时,我在顶部导航栏的右侧看到一个暗影。它在我升级到 Xcode 5.1 后开始。感觉粗糙和分散注意力。我怎样才能摆脱它?

采纳答案by Nihat

self.navigationController.navigationBar.translucent = NO; 

fixed it

修复

回答by nonamelive

self.navigationController.view.backgroundColor = [UIColor whiteColor];

I solved this problem by setting the background color of the navigation controller's view.

我通过设置导航控制器视图的背景颜色解决了这个问题。

回答by manmal

nonamelive's answer is perfect. To achieve the same thing in Interface Builder AND STILL KEEP TRANSLUCENCY, select the navigation controller and set a user defined runtime attribute view.backgroundColoras shown in the screenshot (in the Identity Inspector). Repeat for all navigation controllers that show this problem.

nonamelive的答案是完美的。要在 Interface Builder 中实现相同的操作并保持 TRANSLUCENCY,请选择导航控制器并设置用户定义的运行时属性view.backgroundColor,如屏幕截图(在身份检查器中)所示。对显示此问题的所有导航控制器重复此操作。

It seems that this whole problem occurs because the black color (or actually, no color) of UINavigationController is leaking through at the time CoreGraphics snapshots it at animation begin. So, setting it to white will prevent that.

似乎整个问题的发生是因为 UINavigationController 的黑色(或实际上没有颜色)在 CoreGraphics 在动画开始时对其进行快照时泄漏。因此,将其设置为白色将防止出现这种情况。

Identity Inspector ->User Defined Runtime Attributes

Identity Inspector ->用户定义的运行时属性

回答by tom

This seems to be a bug that was introduced in iOS 7.1. In my case it is caused by a UIToolbar placed directly below the navigation bar. The dark shadow also appears in the translucent tab bar.

这似乎是 iOS 7.1 中引入的一个错误。在我的情况下,它是由直接放置在导航栏下方的 UIToolbar 引起的。黑色阴影也出现在半透明标签栏中。

The shadow seems to be caused by the background view of the UIToolbar. I now use this workaround in the view controller with the toolbar that hides the toolbar's background view during the transition:

阴影似乎是由 UIToolbar 的背景视图引起的。我现在在视图控制器中使用此解决方法,工具栏在转换期间隐藏工具栏的背景视图:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView *toolbarBackgroundView = [self.toolbar findViewRecursively:^BOOL(UIView *subview, BOOL *stop) {
        BOOL isToolbarBackgroundView = ([subview isKindOfClass:[UIImageView class]]
                                        && [NSStringFromClass(subview.class) isEqualToString:@"_UIToolbarBackground"]);
        if (isToolbarBackgroundView) {
            *stop = YES;
        }
        return (! isToolbarBackgroundView);
    }];
    if (toolbarBackgroundView) {
        // fade toolbar background view back in
        [UIView animateWithDuration:0.1f animations:^{
            toolbarBackgroundView.alpha = 1.0f;
        }];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    UIView *toolbarBackgroundView = [self.toolbar findViewRecursively:^BOOL(UIView *subview, BOOL *stop) {
        BOOL isToolbarBackgroundView = ([subview isKindOfClass:[UIImageView class]]
                                        && [NSStringFromClass(subview.class) isEqualToString:@"_UIToolbarBackground"]);
        if (isToolbarBackgroundView) {
            *stop = YES;
        }
        return (! isToolbarBackgroundView);
    }];
    if (toolbarBackgroundView) {
        // hide toolbar background view
        toolbarBackgroundView.alpha = 0.0f;
    }
}

This is the code for [UIView findViewRecursively:]

这是代码 [UIView findViewRecursively:]

@interface UIView (FindSubview)

- (UIView*)findViewRecursively:(BOOL(^)(UIView* subview, BOOL* stop))recurse;

@end

@implementation UIView (FindSubview)

- (UIView*)findViewRecursively:(BOOL(^)(UIView* subview, BOOL* stop))recurse {
    for (UIView* subview in self.subviews) {
        BOOL stop = NO;
        if (recurse(subview, &stop)) {
            UIView* view = [subview findViewRecursively:recurse];
            if (view) return view;
        } else if (stop) {
            return subview;
        }
    }
    return nil;
}

@end

I filed this Radar: http://openradar.appspot.com/16418845

我提交了这个雷达:http: //openradar.appspot.com/16418845

回答by kacho

It seems to happen with any bar (TabBar or ToolBar) that is translucent.
So one way to fix it is to set the _tabBar.translucent = NO;(in my case). This prevents the undesired shadow under the top navigation bar while leaving the navigation bar translucent. Unfortunately the bottom bar is no longer translucent though.

任何半透明的栏(TabBar 或 ToolBar)似乎都会发生这种情况。
所以修复它的一种方法是设置_tabBar.translucent = NO;(在我的情况下)。这可以防止顶部导航栏下方出现不需要的阴影,同时使导航栏保持半透明。不幸的是,底部栏不再是半透明的。

It can be set back to translucent but all this has to happen after the whole pushing animation is finished thus switching this property is well noticeable.

它可以设置回半透明,但所有这些都必须在整个推送动画完成后发生,因此切换此属性非常明显。

In case, however the bottom bar also has to be translucent and I don't want the user to see the change I resolved it with the following:

但是,如果底部栏也必须是半透明的,并且我不希望用户看到更改,我使用以下方法解决了它:

/*  create a simple quick animation of the bottom bar
    just before pushing the new controller */
[UIView animateWithDuration:0.1
                 animations:^{
                     _tabBar.barTintColor = [UIColor colorWithWhite:0.97254901960784 alpha:1.0]; // this is the closest color for my case
                     _tabBar.translucent = NO;
                 } completion:^(BOOL finished) {
                     /* now when the animation that makes the bar not translucent
                        is finished we can push the new controller
                        the controller is instantiated before the animation code */
                     [self.navigationController pushViewController:controller animated:YES];
                 }];

Then in the viewDidAppear:I simply reverts that back:

然后在viewDidAppear:我简单地还原回来:

[UIView animateWithDuration:0.1
             animations:^{
                     _tabBar.barTintColor = nil;
                     _tabBar.translucent = YES;
                 }];

There is just a little change in the appearance especially but it's barely noticeable and it's way better than having the shadow under the navigation bar.

特别是外观上只有一点点变化,但几乎不明显,而且比导航栏下的阴影要好得多。

Hope it'll help others to keep bars translucent until Apple fix this behaviour as bars ARE meant to be hidden in some cases unlike it was suggested in other posts especially for the UITabBar

希望它能帮助其他人保持条形半透明,直到 Apple 修复此行为,因为条形在某些情况下是隐藏的,这与其他帖子中的建议不同,特别是对于 UITabBar

回答by pableiros

This works for me in Swift

这在Swift 中对我有用

In AppDelegateon didFinishLaunchingWithOptionsmethod, I set this:

AppDelegatedidFinishLaunchingWithOptions方法,我设置:

UIApplication.shared.windows.first?.backgroundColor = .white

回答by petrsyn

This works for me on iOS 13with both lightand darkthemes and also on older iOS versions.

这对我的作品在iOS的13黑暗的主题,并在旧的IOS版本。

Add the following code to the AppDelegate to the application(didFinishLaunchingWithOptions)method:

将以下代码添加到application(didFinishLaunchingWithOptions)方法的 AppDelegate 中:

if #available(iOS 13.0, *) {
    window?.backgroundColor = UIColor.systemBackground
} else {
    window?.backgroundColor = UIColor.white
}

回答by Shyam Raju

self.navigationController!.navigationBar.translucent = false;

This works for me place it inside the function where you push the new ViewController

这对我有用,将它放在推送新 ViewController 的函数中

回答by seb

The following also works and leaves the Navigation Bar transparent:

以下也有效并使导航栏保持透明:

[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];

[UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];

回答by user2734823

Here is my variation...it requires much less code than tom's answer, and is more efficient. This is IF you want a translucent navigation bar, and also want to fix that shadow problem.

这是我的变体……它需要的代码比汤姆的答案少得多,而且效率更高。如果您想要一个半透明的导航栏,并且还想解决阴影问题,那就是这样。

In the source ViewController (that is embedded in the Navigation Controller)...

在源 ViewController(嵌入在导航控制器中)中...

- (void)viewDidAppear:(BOOL)animated
{
     self.navigationController.navigationBar.translucent = YES;
}

and

 - (void)viewWillDisappear:(BOOL)animated
 {
     self.navigationController.navigationBar.translucent = NO;
 }

The result is the same as what Tom does (visually, to the end user), and is easier to implement. Hope this helps...

结果与 Tom 所做的相同(视觉上,对最终用户而言),并且更容易实现。希望这可以帮助...