當(dāng)系統(tǒng)升級到iOS11及以上版本時,我們會發(fā)現(xiàn)原先設(shè)置在searchBar中間的placeholder和圖片會往左邊移(沒任何設(shè)置的效果),在這里小編附上了解決的辦法,希望大家能夠得到參考。
效果如下圖:

如何實現(xiàn)
1.加入searchBar控件
小編在這里用的是storyboard,若與小編一樣是用storyboard或者XIB的,記得千萬要設(shè)置placeholder。

2.設(shè)置searchBar
在這里可以根據(jù)自己的需要設(shè)置一下searchBar樣式
// searchBar的外觀設(shè)置
searchBar.layer.masksToBounds = true;
searchBar.layer.cornerRadius = searchBar.frame.size.height / 2;
searchBar.layer.borderWidth = 1;
searchBar.contentMode = .center;
searchBar.layer.borderColor = UIColor.init(red: 197/255.0, green: 199/255.0, blue: 200/255.0, alpha: 1).cgColor;
// searchBar彈出的鍵盤類型設(shè)置
searchBar.returnKeyType = UIReturnKeyType.done;
3.searchBar中textField的設(shè)置
// searchBar中的textField設(shè)置
let searchField = searchBar.value(forKey: "_searchField") as! UITextField;
searchField.setValue(UIFont.systemFont(ofSize: 10), forKeyPath: "_placeholderLabel.font");
searchField.setValue(UIColor.init(red: 70/255.0, green: 70/255.0, blue: 70/255.0, alpha: 1), forKeyPath: "_placeholderLabel.textColor");
4.獲取系統(tǒng)版本號
// 當(dāng)前IOS系統(tǒng)版本
var currentVersion: Int!
------------------------------
// 獲取系統(tǒng)版本號
let sysVersion = UIDevice.current.systemVersion
currentVersion = Int(sysVersion.components(separatedBy: ".").first!)
5.根據(jù)系統(tǒng)版本號判斷操作
let space = 50;//搜索框圖片加上圖片和字體之間的距離
----------------------------------------
// 判斷是不是大于IOS 11
if currentVersion! < 11 {
searchField.attributedPlaceholder = NSAttributedString.init(string: "SEARCH", attributes: [NSAttributedStringKey.baselineOffset:-2]);
}else
{
// searchBar中textField的placeholder的寬度可以獲取
// let label = UILabel.init()
// label.text = "SEARCH"
// label.font = UIFont.systemFont(ofSize: 10)
// label.sizeToFit()
// print(label.frame.width)
// 獲取當(dāng)前屏幕寬度
self.view.frame.size.width = UIScreen.main.bounds.size.width
// 重新布局
self.view.layoutSubviews()
// 計算偏移量:偏移量 =(searchBar的寬度-label寬度-搜索框圖片加上圖片和字體之間的寬度)/ 2
searchBar.setPositionAdjustment(UIOffsetMake((searchBar.frame.size.width - 40.5 - 50 ) / 2 , 0), for: UISearchBarIcon.search)
}
6.在searchBar的代理中進(jìn)行判斷
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
// 輸入時需要進(jìn)行的操作
}
func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
if currentVersion >= 11 {
self.searchBar.setPositionAdjustment(UIOffset.zero, for: UISearchBarIcon.search)
}
return true
}
func searchBarShouldEndEditing(_ searchBar: UISearchBar) -> Bool {
if currentVersion >= 11 {
searchBar.setPositionAdjustment(UIOffsetMake((searchBar.frame.size.width - 40.5 - 50 ) / 2 , 0), for: UISearchBarIcon.search)
}
return true
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
// 輸入完成時,點擊按鈕需要進(jìn)行的操作
searchBar.resignFirstResponder()
}
7.最后,加上點擊空白處關(guān)閉鍵盤就完成了
// 使鍵盤點擊空白處關(guān)閉
let tap = UITapGestureRecognizer.init(target: self, action: #selector(viewTapped(tap:)));
tap.cancelsTouchesInView = false;
self.view.addGestureRecognizer(tap);
}
@objc func viewTapped(tap: UITapGestureRecognizer) {
searchBar.resignFirstResponder()
}
最后,希望這篇文章對各位看官們有所幫助。Demo下載地址:Demo,對支持小編的看官們表示感謝。