iOS UISearchBar search box

// UISearchBar的常用方法 搜索框

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

    oneSearchBar.frame = CGRectMake(0, 0, 320, 70); // 设置位置和大小

    oneSearchBar.keyboardType = UIKeyboardTypeEmailAddress; // 设置弹出键盘的类型

    oneSearchBar.barStyle = UIBarStyleBlackTranslucent; // 设置UISearchBar的样式

    oneSearchBar.tintColor = [UIColor redColor]; // 设置UISearchBar的颜色 使用clearColor就是去掉背景

    oneSearchBar.placeholder = @"请输入:"; // 设置提示文字

    oneSearchBar.text = @"呵呵"; // 设置默认的文字

    oneSearchBar.prompt = @"提示信息"; // 设置提示

    oneSearchBar.delegate = self; // 设置代理

    

    oneSearchBar.showsCancelButton = YES; // 设置时候显示关闭按钮

    // oneSearchBar.showsScopeBar = YES; // 设置显示范围框

    // oneSearchBar.showsSearchResultsButton = YES; // 设置显示搜索结果

    // oneSearchBar.showsBookmarkButton = YES; // 设置显示书签按钮

    

    [self.view addSubview:oneSearchBar]; // 添加到View上

    [oneSearchBar release], oneSearchBar = nil; // 释放内存





////////////////////////一些常用的代理方法//////////////////////////////

 

#pragma mark - 实现取消按钮的方法

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    NSLog(@"您点击了取消按钮");

    [searchBar resignFirstResponder]; // 丢弃第一使用者

}

#pragma mark - 实现键盘上Search按钮的方法

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {

    NSLog(@"您点击了键盘上的Search按钮");

}

#pragma mark - 实现监听开始输入的方法

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {

    NSLog(@"开始输入搜索内容");

    return YES;

}

#pragma mark - 实现监听输入完毕的方法

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {

    NSLog(@"输入完毕");

    return YES;

}

Reproduced in: https: //www.jianshu.com/p/a2baa24988fa

Guess you like

Origin blog.csdn.net/weixin_33743248/article/details/91201553