代码之家  ›  专栏  ›  技术社区  ›  COLD ICE

使用彩色导航栏时,搜索栏中会出现白线

  •  5
  • COLD ICE  · 技术社区  · 7 年前

    单击搜索栏时,我看到多条白线。

    enter image description here

    当同时使用 TabBarController 还有一个彩色条 导航控制器 但是

    • 仅当使用NavigationController时,它才起作用
    • 当TabBarController和NavigationController都使用默认颜色时,它会工作

    enter image description here


    我在中设置导航颜色 AppDelegate 使用此行代码:

    UINavigationBar.appearance().barTintColor =  UIColor(rgb: 0x0277BD)
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
    

    我在我的 SearchViewController 使用:

    let searchController = UISearchController(searchResultsController: nil)
        override func viewDidLoad() {
            super.viewDidLoad()
            // Setup the Search Controller
    
            searchController.searchResultsUpdater = self
            searchController.obscuresBackgroundDuringPresentation = false
            searchController.searchBar.placeholder = "Search Events"
            searchController.searchBar.tintColor = .white
            navigationItem.searchController = searchController
            definesPresentationContext = true
    }
    

    知道发生了什么吗?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Community CDub    5 年前

    不确定这是否是一个令人满意的答案,但它看起来像一个iOS错误 可以 与默认情况下添加到顶部栏的半透明效果有关。顶部栏由两部分组成(导航和搜索),在向上滑动动画期间,导航部分的底部边缘似乎出现了白线。如果您添加 navigationController?.navigationBar.isTranslucent = false 到您的 viewDidLoad() 问题消失了。

    Translucent bar 半透明条

    Opaque bar 不透明条

    为什么只有在嵌入 UINavigationController UITabBarController ? 不知道:( 这个 isTranslucent = false 这充其量只是一种变通方法,但也许已经足够了。

        2
  •  1
  •   Ely    7 年前

    在不放弃半透明性的情况下,一个非常脏的解决方法是添加一个小的“遮罩”视图:

    let rect = CGRect(x: 0, y: navigationController.navigationBar.frame.height, width: navigationController.navigationBar.frame.width, height: 1.0)
    let view = UIView(frame: rect)
    view.backgroundColor = /* Your matching background color */
    view.autoresizingMask = [.flexibleTopMargin]
    navigationController.navigationBar.addSubview(view)
    

    将此添加到ViewDidDisplay作为一次性操作。此解决方法不会在导航转换期间完全隐藏问题。这个问题是一个bug,受此问题影响的每个人都应该向苹果报告。