iOS開發--UIKit控制項之UISearchBar(搜尋欄),--uikituisearchbar
今天因為需求原因,需要用到搜尋控制項;之前一直沒有用到過這個控制項,所以去百度了一下,找到一篇可以說很齊全的資料,感謝這位作者。
然而,我並沒有找到可以更改字型大小的屬性或方法,希望有知道的告訴我一聲,謝謝!
原文地址:http://my.oschina.net/u/2340880/blog/509756
初始化:UISearchBar繼承於UIView,我們可以像建立View那樣建立searchBar
1 UISearchBar *bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 40)];2 [self.view addSubview:bar];
1 // 這個屬性可以設定searchBar的搜尋 2 @property(nonatomic) UIBarStyle barStyle; 3 // 枚舉如下: 4 typedef NS_ENUM(NSInteger, UIBarStyle) { 5 UIBarStyleDefault = 0,//預設風格 白色搜尋方塊,多出的背景為灰色 6 UIBarStyleBlack = 1,//黑色風格,黑色的搜尋方塊 7 //下面兩個枚舉已經被禁用,作用和黑色風格一樣 8 UIBarStyleBlackOpaque = 1, // Deprecated. Use UIBarStyleBlack 9 UIBarStyleBlackTranslucent = 2, // Deprecated. Use UIBarStyleBlack and set the translucent property to YES10 };
設定搜尋方塊中的文字
1 @property(nonatomic,copy) NSString *text;
這個屬性的官方解釋是在搜尋方塊頂部顯示一行文字,其實就是背景文字,說明:
1 @property(nonatomic,copy) NSString *prompt;
1 bar.prompt = @"搜尋方塊";2 bar.text=@"321111111111111111111111111"
效果如下:
和其他文本輸入控制項的placeholder相同,在輸入文字時就會消失
1 @property(nonatomic,copy) NSString *placeholder;
是否在搜尋方塊右側顯示一個圖書的按鈕,預設為NO,YES的效果如下:
1 @property(nonatomic) BOOL showsBookmarkButton;
是否顯示取消按鈕,預設為NO,YES的效果如下:
1 @property(nonatomic) BOOL showsCancelButton;
是否顯示搜尋結果按鈕,預設為NO,YES效果如下:
1 @property(nonatomic) BOOL showsSearchResultsButton;
設定搜尋結果按鈕的選中狀態
1 @property(nonatomic, getter=isSearchResultsButtonSelected) BOOL searchResultsButtonSelected ;
設定顯示取消按鈕
1 - (void)setShowsCancelButton:(BOOL)showsCancelButton animated:(BOOL)animated;
設定這個顏色值會影響搜尋方塊中的游標的顏色
1 @property(nonatomic,retain) UIColor *tintColor;
設定這個顏色會影響搜尋方塊的背景顏色
1 @property(nonatomic,retain) UIColor *barTintColor;
設定搜尋方塊整體的風格
1 @property (nonatomic) UISearchBarStyle searchBarStyle;
枚舉如下:
1 typedef NS_ENUM(NSUInteger, UISearchBarStyle) {2 UISearchBarStyleDefault, // currently UISearchBarStyleProminent3 UISearchBarStyleProminent, // 顯示背景4 UISearchBarStyleMinimal // 不顯示背景5 } NS_ENUM_AVAILABLE_IOS(7_0);
設定是否半透明
1 @property(nonatomic,assign,getter=isTranslucent) BOOL translucent;
是否顯示搜尋欄的附件選擇按鈕視圖,要想顯示這個視圖,首先要將這個屬性設定為YES,之後給按鈕數組中添加按鈕,使用下面這個屬性:
1 @property(nonatomic) BOOL showsScopeBar ;
設定選擇按鈕視圖的按鈕標題
1 @property(nonatomic,copy) NSArray *scopeButtonTitles ;2 3 @property(nonatomic) NSInteger selectedScopeButtonIndex;
設定一個預設的選中按鈕
bar = [[UISearchBar alloc]initWithFrame:CGRectMake(20, 100, 250, 200)];bar.showsScopeBar=YES;bar.scopeButtonTitles = @[@"12",@"2",@"3",@"4"];
鍵盤的附屬視圖
1 @property (nonatomic, readwrite, retain) UIView *inputAccessoryView;
設定搜尋方塊的背景圖案
1 @property(nonatomic,retain) UIImage *backgroundImage;
設定附屬選擇按鈕視圖的背景圖案
1 @property(nonatomic,retain) UIImage *scopeBarBackgroundImage;
這一對方法可以設定和擷取某個狀態枚舉下的搜尋方塊的背景圖案
- (void)setBackgroundImage:(UIImage *)backgroundImage forBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics ;
- (UIImage *)backgroundImageForBarPosition:(UIBarPosition)barPosition barMetrics:(UIBarMetrics)barMetrics
這一對方法用於設定和擷取搜尋方塊中TextField的背景圖案
- (void)setSearchFieldBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state;
- (UIImage *)searchFieldBackgroundImageForState:(UIControlState)state;
這一對方法用於擷取和設定搜尋欄icon圖片的圖案
- (void)setImage:(UIImage *)iconImage forSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ;
- (UIImage *)imageForSearchBarIcon:(UISearchBarIcon)icon state:(UIControlState)state ;
這一對方法用於設定和擷取搜尋方塊的附加選擇按鈕視圖的背景圖案
- (void)setScopeBarButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state;
- (UIImage *)scopeBarButtonBackgroundImageForState:(UIControlState)state;
這一對方法用於擷取和設定附加選擇按鈕視圖中切換按鈕的圖案
- (void)setScopeBarButtonDividerImage:(UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
- (UIImage *)scopeBarButtonDividerImageForLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState;
這一對方法用於設定和擷取切換按鈕標題文字的字型屬性字典
- (void)setScopeBarButtonTitleTextAttributes:(NSDictionary *)attributes forState:(UIControlState)state;
- (NSDictionary *)scopeBarButtonTitleTextAttributesForState:(UIControlState)state;
搜尋文字在搜尋方塊中的位置位移
@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment;
textfield在搜尋方塊中的位置位移
@property(nonatomic) UIOffset searchTextPositionAdjustment;
設定搜尋欄中圖片的位置位移
- (void)setPositionAdjustment:(UIOffset)adjustment forSearchBarIcon:(UISearchBarIcon)icon;
- (UIOffset)positionAdjustmentForSearchBarIcon:(UISearchBarIcon)icon;
圖片的枚舉如下:
typedef NS_ENUM(NSInteger, UISearchBarIcon) { UISearchBarIconSearch, //搜尋表徵圖 UISearchBarIconClear, // 清除表徵圖 UISearchBarIconBookmark, // 書本表徵圖 UISearchBarIconResultsList, // 結果清單表徵圖};
下面是搜尋方塊控制項的一些代理方法:
將要開始編輯時的回調,返回為NO,則不能編輯
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;
已經開始編輯時的回調
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;
將要結束編輯時的回調
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;
已經結束編輯的回調
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;
編輯文字改變的回調
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;
編輯文字改變前的回調,返回NO則不能加入新的編輯文字
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text ;
搜尋按鈕點擊的回調
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;
書本按鈕點擊的回調
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;
取消按鈕點擊的回調
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;
搜尋結果按鈕點擊的回調
- (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar;
搜尋欄的附加視圖中切換按鈕觸發的回調
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope;
2016-07-24