这里我刚刚在Xcode中放置了一个默认设计的搜索栏控制器。任何会帮助我让搜索栏像第二个?
默认
预期输出/用户界面设计
在这里,我附上了自己的UITextField设计,它可以帮助您解决这个问题:Github
答案 0 :(得分:1)
我已将UISearchBar
添加到UINavigationBar
。
自定义:
@IBOutlet weak var BGView: UIView! //CONSTRAINTS top, left and right = 0, height = 64
@IBOutlet var topSearchBarr: UISearchBar!
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.view.backgroundColor = .clear
let gradient: CAGradientLayer = CAGradientLayer()
//Set the two colors of your gradient
gradient.colors = [UIColor.purple.cgColor, UIColor.red.cgColor]
//Set it's location
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 0.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.size.width, height: 64)
BGView.layer.insertSublayer(gradient, at: 0)
getSearchBarSubViews()
}
func getSearchBarSubViews()
{
let navSubVws = self.navigationController?.navigationBar.subviews
for subVws in navSubVws!
{
if (String(describing: subVws).range(of:"UINavigationBarContentView") != nil)
{
let barContentSubVws = subVws.subviews
for subVws in barContentSubVws
{
if (String(describing: subVws).range(of:"UISearchBar") != nil)
{
let searchbarVws = subVws.subviews
for subVws in searchbarVws
{
if (String(describing: subVws).range(of:"UIView") != nil)
{
let searchbarTopSubVws = subVws.subviews
for subVws in searchbarTopSubVws
{
if (String(describing: subVws).range(of:"UISearchBarTextField") != nil)
{
let searchBarVw = subVws as! UITextField
let searchbarTxtFldSubVws = subVws.subviews
(subVws as! UITextField).backgroundColor = UIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1.0)
(subVws as! UITextField).tintColor = UIColor.white
(subVws as! UITextField).textColor = UIColor.white
(subVws as! UITextField).clearButtonMode = .never
for subVws in searchbarTxtFldSubVws
{
if (String(describing: subVws).range(of:"UISearchBarTextFieldLabel") != nil)
{
(subVws as! UILabel).textColor = UIColor.white
}
if (String(describing: subVws).range(of:"UIImageView") != nil)
{
var searchIconImgVw = (subVws as! UIImageView)
searchIconImgVw.image = searchIconImgVw.image!.withRenderingMode(.alwaysTemplate)
searchIconImgVw.tintColor = UIColor.white
searchBarVw.leftViewMode = .never
searchBarVw.rightView = searchIconImgVw
searchBarVw.rightViewMode = .always
}
}
}
}
}
}
}
}
}
}
}
<强>故事板强>
输出1
输出2