今天公司的項目測試的差不多了,基本可以上架了物延,又有時間來分享一下最近遇到的一些問題了宣旱,公司的項目進(jìn)行了大改版(應(yīng)該是全改了,基本是一個新的項目了)叛薯,老大決定用swift重寫浑吟。之前一直在自學(xué)swift,終于這一次可以實戰(zhàn)了耗溜。項目中搜索用的比較多组力,但是搜索框的樣式與默認(rèn)的差別太大了,所以只能自定義了抖拴。
<p>The UISearchBar class implements a text field control for text-based searches. The control provides a text field for entering text, a search button, a bookmark button, and a cancel button. The UISearchBar object does not actually perform any searches. You use a delegate, an object conforming to the UISearchBarDelegate protocol, to implement the actions when text is entered and buttons are clicked.<p>
以上是蘋果對UISearchBar的解釋燎字,可以看見UISearchBar提供了類似UITextField的輸入(其實它的組成中就有UITextField腥椒,下面會講到),右邊有搜索按鈕候衍、標(biāo)簽按鈕笼蛛、關(guān)閉按鈕可供選擇,搜索都是在協(xié)議UISearchBarDelegate中進(jìn)行蛉鹿。
1.自定義外觀
UISearchBar的層級很是復(fù)雜主要由UISearchBarBackgroud滨砍、UISearchBarTextField、UINavigationButton組成妖异,其中UISearchBarTextField就是輸入框惋戏,主要是由——UISearchBarSearchFieldBackgroundView、UIButton(?)随闺、UIImageView(??)等組成日川,獲取TextField方法:
let searchFiled:UITextField = self.searchBar.value(forKey: "_searchField") as! UITextField
這樣就可以通過修改 searchFiled來修改輸入樣式(圓角蔓腐、字體等)矩乐。
UISearchBar的直接子控件是UIVIew,其上的子控件UISearchBarBackgroud的frame與UISearchBar的bounds相等回论,UISearchBarTextField的高度默認(rèn)為28與UISearchBar左右有8像素的固定間距散罕,上下間距為直接子控件UIView的高度 - UISearchBarTextField的默認(rèn)高度28 再除以2。因此UISearchBar的輸入框始終與設(shè)置的frame不一樣傀蓉,不便于布局欧漱,我們可以添加一個子類繼承UISearchBar,可以更改其內(nèi)邊距葬燎。
class MySearchBar: UISearchBar {
// 監(jiān)聽是否添加了該屬性
var contentInset: UIEdgeInsets? {
didSet {
self.layoutSubviews()
}
}
override func layoutSubviews() {
super.layoutSubviews()
// 便利尋找
for view in self.subviews {
for subview in view.subviews {
// 判定是否是UISearchBarTextField
if subview.isKind(of: UITextField.classForCoder()) {
if let textFieldContentInset = contentInset {
// 修改UISearchBarTextField的布局
subview.frame = CGRect(x: textFieldContentInset.left, y: textFieldContentInset.top, width: self.bounds.width - textFieldContentInset.left - textFieldContentInset.right, height: self.bounds.height - textFieldContentInset.top - textFieldContentInset.bottom)
} else {
// 設(shè)置UISearchBar中UISearchBarTextField的默認(rèn)邊距
let top: CGFloat = (self.bounds.height - 28.0) / 2.0
let bottom: CGFloat = top
let left: CGFloat = 8.0
let right: CGFloat = left
contentInset = UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
}
}
}
}
}
}
讓實例化的UISearchBar繼承MySearchBar误甚,然后就可以很方便的直接控制內(nèi)邊距了
self.searchBar.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
接下來就是處理placeholder靠左,這個就比較麻煩了谱净,查詢了一大堆辦法都挺麻煩的窑邦,最后找到了一個很投機(jī)的辦法:先判定手機(jī)寬度,然后在placeholder右邊加上空格做成靠左的假象壕探。
if SCREEN.WIDTH == 320 {
self.searchBar.placeholder = "搜索位置 "
}else if SCREEN.WIDTH == 373\5 {
self.searchBar.placeholder = "搜索位置 "
}else if SCREEN.WIDTH == 414 {
self.searchBar.placeholder = "搜索位置 "
}
然后在storyboard中設(shè)置searchBar的BarStyle為Minimal就可以很方便的控制UISearchBar的外觀了冈钦。
到這里就剩一個問題了:UISearchBar上下的兩根黑線了,去除方法:
self.searchBar.setBackgroundImage(UIImage.init(), for: .any, barMetrics: .default)
2.搜索的使用
如蘋果官方文檔所說李请,與搜索相關(guān)的都是在其代理方法中完成瞧筛。UISearchBar有很多的代理方法,感興趣的可以點(diǎn)擊進(jìn)入查看UISearchBarDelegate我就介紹幾個常用的:
當(dāng)搜索內(nèi)容變化時导盅,執(zhí)行該方法,可以實時監(jiān)聽輸入框的變化,可以實現(xiàn)時實搜索较幌。
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)textNS_AVAILABLE_IOS(3_0); // called before text changes
也行你想把搜索事件放在點(diǎn)擊搜索以后再觸發(fā),那就選用這個方法白翻,它就是點(diǎn)擊搜索后的代理方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
3.結(jié)束
當(dāng)然如果你覺得這樣太麻煩了乍炉,你還可以選擇用UITextField來實現(xiàn)UISearchBar的功能,但是最終哪一個更加的麻煩還需要試一試才知道。