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

COL*_*ICE 5 uitabbarcontroller uinavigationcontroller uisearchbar swift

单击 SearchBar 时出现多条白线。

在此处输入图片说明

同时使用TabBarController和彩色条NavigationController时会发生这种情况,但是

  • 它仅在使用 NavigationController 时有效
  • 它适用于 TabBarController 和 NavigationController 但使用默认颜色

在此处输入图片说明


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

UINavigationBar.appearance().barTintColor =  UIColor(rgb: 0x0277BD)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
Run Code Online (Sandbox Code Playgroud)

我使用以下方法在SearchViewController 中设置了 UISearchController :

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
}
Run Code Online (Sandbox Code Playgroud)

知道发生了什么吗?

Ale*_*Maj 3

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

半透明条 半透明条

不透明条 不透明条

UINavigationController为什么只有在嵌入时才会出现白线UITabBarController不知道:(这isTranslucent = false件事充其量只是一个解决方法,但也许这就足够了。