UISearchBar常用的属性方法

来源:互联网 发布:sql primary key 编辑:程序博客网 时间:2024/06/07 00:50

一 、UISearchBar常用属性

    // 创建UISearchBar

    UISearchBar *searchBar = [[UISearchBaralloc] init];

    // 自适应大小

    [searchBar sizeToFit];

    // 1.设置搜索框的样式

    [searchBar setBarStyle:UIBarStyleDefault];

    // 2.设置背景图片(该方法可以去掉UISearchBar上下的两条线)

    searchBar.backgroundImage = [UIImageimageNamed:@"search_bg_icon"];

    // 3.设置主题颜色

    searchBar.barTintColor = [UIColorredColor];

    // 4.设置外边框颜色

    searchBar.barTintColor = [UIColorgreenColor];

    // 5.设置光标颜色

    searchBar.tintColor = [UIColorcyanColor];

    // 6.设置是否透明

    searchBar.translucent =YES;

    // 7.设置占位文字

    searchBar.placeholder =@"占位文字";

    // 8.输入框中间的提示文字

    searchBar.prompt =@"提示文字";

    // 9.显示搜索框右侧的搜索结果按钮

    searchBar.showsSearchResultsButton =YES;

    // 10.搜索框右侧的搜索结果按钮是否选中

    searchBar.searchResultsButtonSelected =YES;

    // 11.设置UISearchBar背景的偏移量

    searchBar.searchFieldBackgroundPositionAdjustment =UIOffsetMake(50,20);

    // 12.设置UISearchBar开始编辑时文本的偏移量

    searchBar.searchTextPositionAdjustment =UIOffsetMake(50,20);

    // 13.开始编辑时键盘上方出现一个遮盖视图

    UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 0,266)];

    view.backgroundColor = [UIColoryellowColor];

    searchBar.inputAccessoryView = view;

    // 14.设置键盘的样式

    searchBar.keyboardType =UIKeyboardTypeASCIICapable;

    // 15.是否显示搜索框下面的选项条

    searchBar.showsScopeBar =YES;

    // 16.搜索框下面选项条中选项的名称

    searchBar.scopeButtonTitles =@[@"aaaa",@"bbbb",@"cccc"];

    // 17.选项条的背景图片

    searchBar.scopeBarBackgroundImage = [UIImageimageNamed:@"ios_v4_preview_2"];

    // 18.选项条默认选中的按钮下标

    searchBar.selectedScopeButtonIndex =1;

    // 19.显示输入框右侧的书形图标

    searchBar.showsBookmarkButton =YES;

    // 20.显示右侧的取消按钮(无动画)

//    searchBar.showsCancelButton = YES;

    // 21.显示右侧的取消按钮(有动画)

    [searchBar setShowsCancelButton:YESanimated:YES];



二、UISearchBar的代理方法


// 开始编辑时会来到该方法

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar

// 结束编辑时会来到该方法

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar

// 开始编辑时会来到该方法(可以在该方法判断是否允许用户输入)

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar

// 结束编辑时会来到该方法

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar

// 点击取消按钮时会来到该方法

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar

// 点击键盘的搜索按钮时会来到该方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar

// 输入框内容发生改变时,会来到该方法

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText




0 0
原创粉丝点击