iOS UISearchBar简单使用与简介

import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor whiteColor];

    UISearchBar * mySearchBar = [[UISearchBar alloc] initWithFrame:(CGRectMake(0, 22, [UIScreen mainScreen].bounds.size.width, 41))];
    mySearchBar.delegate = self;

    mySearchBar.backgroundColor = [UIColor redColor];

    // 控件样式
    mySearchBar.barStyle = UIBarStyleDefault;
    mySearchBar.keyboardType = UIKeyboardTypeDefault;
    // 占位符
    mySearchBar.placeholder = @”输入搜索内容”;

// mySearchBar.showsBookmarkButton = YES;
// mySearchBar.showsCancelButton = YES;

[self.view addSubview:mySearchBar];

}

pragma mark – 代理方法

  • (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
    {
    NSLog(@”开始输入”); // 第一步
    return YES;
    }

  • (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
    {
    NSLog(@”开始编辑UISearchBar 的 textView”); // 第二步
    }

  • (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
    {
    NSLog(@”结束输入”);
    return YES;
    }

// 当textView的文字改变或者清除的时候调用此方法, 搜若栏目目前的状态正在编辑, 在搜索文字字段中的当前文本
- (void)searchBar:(UISearchBar )searchBar textDidChange:(NSString )searchText
{
NSLog(@”时刻显示输入框中的内容——%@”, searchText);
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
NSLog(@”结束编辑输入”);

}

  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
    // 当点击search的时候调用
    NSLog(@”点击搜索按钮”);
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

UISearchBar 属性介绍:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值