OGeek|极客世界-中国程序员成长平台

标题: ios - 带有大标题的 UINavigationBar 没有背景图片 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:19
标题: ios - 带有大标题的 UINavigationBar 没有背景图片

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

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 不显示大标题时才会显示背景。我不知道我是否必须更改其他属性,否则这不适用于大型标题。任何帮助将不胜感激,在此先感谢。



Best Answer-推荐答案


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

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

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

关于ios - 带有大标题的 UINavigationBar 没有背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157306/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4