[英]How to set border bottom on UISearchBar in Swift3
我想指定 UISearchBar 的底部边框颜色。 我已经看到很多答案可以帮助摆脱边框,或者将边框颜色设置为一个整体,但我只想为底部边框设置边框颜色。 这是我的代码:
searchController.searchBar.layer.borderWidth = 1 searchController.searchBar.layer.borderColor = UIColor.white.cgColor
有没有办法只为底部边框设置 borderColor ?
如果您使用的是低于 3(<3.0) 的 swift 版本,那么下面的代码应该可以工作 -
let border = CALayer() let width = CGFloat(2.0) border.borderColor = UIColor.red.cgColor border.frame = CGRect(x: 0, y: searchBar.frame.size.height + width, width: searchBar.frame.size.width, height: searchBar.frame.size.height) border.borderWidth = width searchBar.layer.addSublayer(border) searchBar.layer.masksToBounds = true
我正在使用 swift 3.0,但在添加此评论之前,我最终测试了此功能,但它对我不起作用。 我不知道为什么,但这是在任何元素底部显示边框的正确代码。
我尝试在搜索栏底部使用 insertinglayout 但它不起作用。
self.view.layer.insertSublayer(border, below:searchBar.layer)
我不会告诉你这是一个答案。 但这会让您了解如何在特定项目的底部添加布局。
只需像这样向您的 textField 添加一个子视图
let frame = CGRectMake(0, textField.frame.size.height - 1, textField.frame.size.width, 1)
let border = UIView(frame: frame)
border.backgroundColor = UIColor.whiteColor()
textField.addSubview(border)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.