带有大标题的 UINavigationBar 没有背景图片

Posted

技术标签:

【中文标题】带有大标题的 UINavigationBar 没有背景图片【英文标题】:UINavigationBar with large title does not have background image 【发布时间】:2018-07-03 14:40:04 【问题描述】:

我在更新应用程序以使 UINavigationBar 实例显示大标题时遇到了问题。问题是在我的应用程序的某些屏幕中,我不希望UINavigationBar 具有shadowbackgroundColor,因此我有一种方法可以将其外观从“固态”状态更改为“透明” “一,倒退。代码如下:

public static func setNavAppearance(type: AppearanceType, navigationBar: UINavigationBar?) 

    if (type == .transparent) 
        navigationBar?.setBackgroundImage(UIImage.init(), for: .default)
        navigationBar?.shadowImage = UIImage.init()
        navigationBar?.isTranslucent = true
    
    else 
        navigationBar?.shadowImage = nil
        navigationBar?.setBackgroundImage(nil, for: UIBarMetrics.default)
        navigationBar?.isTranslucent = false
    

如您所见,此方法所做的只是更改translucent 属性并设置/取消设置shadowImagebackgroundImage。这在不使用大标题的情况下工作得很好,我附上设置透明外观的图像:

UINavigationBar without background image and shadow, but showing UIBarButtonItems and backButton

对于大标题,这仍然可以正常工作;将外观类型设置回“实心”后出现问题。我再附上两张显示问题的图片:

UINavigationBar still doesn't have background

After scrolling up a bit so that large title is collapsed, background color appears

所以问题是,只有在navigationBar 不显示大标题时才会显示该背景。我不知道我是否必须更改其他属性,否则这不适用于大型标题。任何帮助将不胜感激,在此先感谢。

【问题讨论】:

我也有类似的问题,请问有解决方法吗? 你解决了吗? @Eduard 不,我还没有找到任何有效的解决方案。我所做的是在推送时隐藏导航栏,然后使用情节提要将导航栏添加到视图控制器,并自定义该导航栏。返回的交互手势丢失了,但至少是一种按照我们想要的方式自定义导航栏的方式。 【参考方案1】:

我也遇到了这个问题。我的解决方法是将 UINavigationController 的 UIView 的背景颜色设置为 UINavigationBar 应该具有的背景颜色。

然后将 UINavigationBar 颜色设置为 UIClear 和半透明,并确保带有大标题的 UIViewController 不应在顶栏下方延伸其边缘。

基本上就是将导航控制器的背景设置为导航栏的背景,并将顶部的所有背景设置为半透明。

【讨论】:

以上是关于带有大标题的 UINavigationBar 没有背景图片的主要内容,如果未能解决你的问题,请参考以下文章

UINavigationBar 大标题重影

iPhone - 使用 Interface Builder 创建一个带有后退按钮的简单 UINavigationBar

iOS 11 中 UINavigationBar 大标题的默认字体?

带有 RECurtainViewController 的 UINavigationBar 外观

防止 UISearchBarController 显示 UINavigationBar

带有 UINavigationBar 的 UIPageViewController 内的 UIWebView