searchbar的使用介绍

searchBar的使用介绍

首先如何创建一个SearchBar实例:

self.searchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(0.0, 20, self.view.bounds.size.width, 40)];
    self.searchBar.placeholder=@"搜索";
    self.searchBar.showsCancelButton = YES;//显示右边的取消按钮。
    self.searchBar.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;//自动修正
    self.searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;//首字母大写

我们还需要设置代理,然后实现这个里面的方法:

    //遵循协议:UISearchBarDelegate
    下面是这个协议的方法:
    @protocol UISearchBarDelegate <UIBarPositioningDelegate>

    @optional

    - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar;                      // return NO to not become first responder
    - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;                     // called when text starts editing
    - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar;                        // return NO to not resign first responder
    - (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar;                       // called when text ends editing
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
    - (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text NS_AVAILABLE_IOS(3_0); // called before text changes

    - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     // called when keyboard search button pressed
    - (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar;                   // called when bookmark button pressed
    - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar;                     // called when cancel button pressed
    - (void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar NS_AVAILABLE_IOS(3_2); // called when search results button pressed

    - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope NS_AVAILABLE_IOS(3_0);

    @end

如何消除searchBar中的背景:

    //    如何将背景消除:首先我们在调试窗口,打出self.searchBar的层次结构。然后根据这个结构将UISearchBar中的背景视图给remove。我们这样就实现删除背景颜色的效果
    [[[[self.searchBar.subviews objectAtIndex:0] subviews] objectAtIndex:0] removeFromSuperview];

如何打出层次结构:

在命令行中,使用recursiveDescription打印出UIView的层次结构,
例如:

po [self.searchBar recursiveDescription]
    //打印出得结果为:
    <UISearchBar: 0x7ba7f6d0; frame = (0 20; 320 40); text = ''; gestureRecognizers = <NSArray: 0x7ba8cf20>; layer = <CALayer: 0x7ba881f0>>
       | <UIView: 0x7ba8b530; frame = (0 0; 320 40); clipsToBounds = YES; autoresize = W+H; layer = <CALayer: 0x7ba8b5a0>>
       |    | <UISearchBarBackground: 0x7ba8d480; frame = (0 0; 320 40); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x7ba8d670>>
       |    | <UISearchBarTextField: 0x7b7745a0; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; layer = <CALayer: 0x7b7737e0>>
       |    |    | <_UISearchBarSearchFieldBackgroundView: 0x7b778390; frame = (0 0; 0 0); opaque = NO; autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x7b777df0>>
posted @ 2015-04-15 14:46  AbeDay  阅读(447)  评论(0编辑  收藏  举报