使用彩色导航栏时 SearchBar 出现白线
White lines appear in SearchBar when using colored navigation bar
我在搜索栏中单击时出现多条白线。
当同时使用 TabBarController 和彩色条 NavigationController 时会发生这种情况,但是
- 仅在使用 NavigationController 时有效
- 在 TabBarController 和 NavigationController 都使用默认颜色时有效
我使用这行代码在 AppDelegate 中设置导航颜色:
UINavigationBar.appearance().barTintColor = UIColor(rgb: 0x0277BD)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
我在我的 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
}
知道发生了什么吗?
不确定这是否是一个令人满意的答案,但它看起来像一个 iOS 错误,可能 与默认添加的半透明效果有关顶栏。顶部栏由两部分组成(导航和搜索),在向上滑动动画期间,白线似乎出现在导航部分的底部边缘。如果将 navigationController?.navigationBar.isTranslucent = false
添加到 viewDidLoad()
,问题就会消失。
半透明条
不透明栏
为什么只有在UITabBarController
中嵌入UINavigationController
才会出现白线?不知道:(
isTranslucent = false
充其量只是一种解决方法,但也许就足够了。
在不放弃半透明性的情况下,一个非常肮脏的解决方法是添加一个小 'masking' 视图:
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)
将此作为一次性操作添加到 viewDidAppear。此解决方法并不能完全隐藏导航转换期间的问题。此问题是一个错误,受此问题影响的每个人都应向 Apple 报告。
我在搜索栏中单击时出现多条白线。
当同时使用 TabBarController 和彩色条 NavigationController 时会发生这种情况,但是
- 仅在使用 NavigationController 时有效
- 在 TabBarController 和 NavigationController 都使用默认颜色时有效
我使用这行代码在 AppDelegate 中设置导航颜色:
UINavigationBar.appearance().barTintColor = UIColor(rgb: 0x0277BD)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
我在我的 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
}
知道发生了什么吗?
不确定这是否是一个令人满意的答案,但它看起来像一个 iOS 错误,可能 与默认添加的半透明效果有关顶栏。顶部栏由两部分组成(导航和搜索),在向上滑动动画期间,白线似乎出现在导航部分的底部边缘。如果将 navigationController?.navigationBar.isTranslucent = false
添加到 viewDidLoad()
,问题就会消失。
为什么只有在UITabBarController
中嵌入UINavigationController
才会出现白线?不知道:(
isTranslucent = false
充其量只是一种解决方法,但也许就足够了。
在不放弃半透明性的情况下,一个非常肮脏的解决方法是添加一个小 'masking' 视图:
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)
将此作为一次性操作添加到 viewDidAppear。此解决方法并不能完全隐藏导航转换期间的问题。此问题是一个错误,受此问题影响的每个人都应向 Apple 报告。