Swift 如何去掉 searchController 的底部边框?
Swift How remove searchController bottom border?
这个页面有tableView和SearchController。我的问题是 searchController 边框。我无法删除边框
我试过:
Remove navigation bar bottom line when using search controller
How to hide UINavigationBar 1px bottom line
How can I remove border bottom of UINavigationBar?
if #available(iOS 11.0, *) {
let scb = searchController.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
scb.layer.masksToBounds = true
scb.layer.borderWidth = 10
scb.layer.borderColor = UIColor.clear.cgColor
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.layer.borderWidth = 2
textfield.layer.borderColor = UIColor.clear.cgColor
//textfield.textColor = // Set text color
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.borderWidth = 0
backgroundview.layer.borderColor = UIColor.clear.cgColor
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.clear
}
navigationItem.hidesSearchBarWhenScrolling = false
}
navigationItem.searchController = searchController
navigationItem.largeTitleDisplayMode = .never
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
如何解决这个问题?有什么想法吗?
一种解决方案是将导航栏的背景和阴影图像设置为空图像。
我又改了一个,评论一下
navigationItem.largeTitleDisplayMode = .never
并添加这两行
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
完整代码如下:
if #available(iOS 11.0, *) {
let scb = searchController.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
scb.layer.masksToBounds = true
scb.layer.borderWidth = 10
scb.layer.borderColor = UIColor.clear.cgColor
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.layer.borderWidth = 2
textfield.layer.borderColor = UIColor.clear.cgColor
//textfield.textColor = // Set text color
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.borderWidth = 0
backgroundview.layer.borderColor = UIColor.clear.cgColor
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.white
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
}
navigationItem.hidesSearchBarWhenScrolling = false
}
// navigationItem.largeTitleDisplayMode = .never
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
}
}
请检查更新后的代码。
创建 UISearchController 的扩展:
extension UISearchController {
var hairlineView: UIView? {
let barBackgroundView = searchBar.superview?.subviews.first { String(describing: type(of: [=10=])) == "_UIBarBackground" }
guard let background = barBackgroundView else { return nil }
return background.subviews.first { [=10=].bounds.height == 1 / self.traitCollection.displayScale }
}
}
在视图中调用它会布局子视图:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationItem.searchController?.hairlineView?.isHidden = true
}
这个页面有tableView和SearchController。我的问题是 searchController 边框。我无法删除边框
我试过:
Remove navigation bar bottom line when using search controller
How to hide UINavigationBar 1px bottom line
How can I remove border bottom of UINavigationBar?
if #available(iOS 11.0, *) {
let scb = searchController.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
scb.layer.masksToBounds = true
scb.layer.borderWidth = 10
scb.layer.borderColor = UIColor.clear.cgColor
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.layer.borderWidth = 2
textfield.layer.borderColor = UIColor.clear.cgColor
//textfield.textColor = // Set text color
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.borderWidth = 0
backgroundview.layer.borderColor = UIColor.clear.cgColor
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.clear
}
navigationItem.hidesSearchBarWhenScrolling = false
}
navigationItem.searchController = searchController
navigationItem.largeTitleDisplayMode = .never
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
如何解决这个问题?有什么想法吗?
一种解决方案是将导航栏的背景和阴影图像设置为空图像。
我又改了一个,评论一下
navigationItem.largeTitleDisplayMode = .never
并添加这两行
navigationbar.setBackgroundImage(UIImage(), for: .default) navigationbar.shadowImage = UIImage()
完整代码如下:
if #available(iOS 11.0, *) {
let scb = searchController.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
scb.layer.masksToBounds = true
scb.layer.borderWidth = 10
scb.layer.borderColor = UIColor.clear.cgColor
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.layer.borderWidth = 2
textfield.layer.borderColor = UIColor.clear.cgColor
//textfield.textColor = // Set text color
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
backgroundview.layer.borderWidth = 0
backgroundview.layer.borderColor = UIColor.clear.cgColor
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.white
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
}
navigationItem.hidesSearchBarWhenScrolling = false
}
// navigationItem.largeTitleDisplayMode = .never
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: .bold) ]
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
definesPresentationContext = true
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.setBackgroundImage(UIImage(), for: .default)
navigationbar.shadowImage = UIImage()
}
}
请检查更新后的代码。
创建 UISearchController 的扩展:
extension UISearchController {
var hairlineView: UIView? {
let barBackgroundView = searchBar.superview?.subviews.first { String(describing: type(of: [=10=])) == "_UIBarBackground" }
guard let background = barBackgroundView else { return nil }
return background.subviews.first { [=10=].bounds.height == 1 / self.traitCollection.displayScale }
}
}
在视图中调用它会布局子视图:
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
self.navigationItem.searchController?.hairlineView?.isHidden = true
}