UISearchBar常用的属性方法

一 、UISearchBar常用属性

    // 创建UISearchBar

    UISearchBar *searchBar = [[UISearchBar alloc] init];

    // 自适应大小

    [searchBar sizeToFit];

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

    [searchBar setBarStyle:UIBarStyleDefault];

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

    searchBar.backgroundImage = [UIImage imageNamed:@"search_bg_icon"];

    // 3.设置主题颜色

    searchBar.barTintColor = [UIColor redColor];

    // 4.设置外边框颜色

    searchBar.barTintColor = [UIColor greenColor];

    // 5.设置光标颜色

    searchBar.tintColor = [UIColor cyanColor];

    // 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 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 266)];

    view.backgroundColor = [UIColor yellowColor];

    searchBar.inputAccessoryView = view;

    // 14.设置键盘的样式

    searchBar.keyboardType = UIKeyboardTypeASCIICapable;

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

    searchBar.showsScopeBar = YES;

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

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

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

    searchBar.scopeBarBackgroundImage = [UIImage imageNamed:@"ios_v4_preview_2"];

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

    searchBar.selectedScopeButtonIndex = 1;

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

    searchBar.showsBookmarkButton = YES;

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

//    searchBar.showsCancelButton = YES;

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

    [searchBar setShowsCancelButton:YES animated: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值