欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UISearchBar外观和Delegate

程序员文章站 2022-07-15 19:56:23
...

初始化

    内部搜索栏高度不随frame改变而改变

UISearchBar * searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(5,50,self.view.frame.size.width - 10,120)];

 

颜色设置

searchBar.barStyle = UIBarStyleBlack;   //黑色风格  默认是白色

searchBar.searchBarStyle = UISearchBarStyleDefault;


// 部件选中时字体变色
searchBar.tintColor = [UIColor blueColor];
    
searchBar.barTintColor = [UIColor grayColor];

 

searchBar部件

searchBar.placeholder = @"请输入数字";    //提示,输入时消失

[searchBar setShowsCancelButton:YES];   //右侧取消按钮
[searchBar setShowsSearchResultsButton:YES];     //搜索栏最右侧箭头按钮
[searchBar setShowsBookmarkButton:YES];     //搜索栏右侧书按钮
//SearchResultsButton和BookmarkButton只能显示一个
    
    
searchBar.showsScopeBar = YES;   //需要先写 ,显示搜索栏下部选择栏
[searchBar setScopeButtonTitles:[[NSArray alloc] initWithObjects:@"one", @"tow", @"three", nil]];    //选择烂标题
searchBar.selectedScopeButtonIndex = 2;    //索引

 searchBar Delegate

searchBar.delegate = self;

// UISearchBar得到焦点并开始编辑时,执行该方法
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
        return YES;
}

//开始编辑时
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{         
          [searchBar setShowsCancelButton:YES animated:YES];   //  动画显示取消按钮
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
        return YES;
} 

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

}

//当搜索框中 文本改变时调用,可以实现实时搜索
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{   

}

 

相关标签: iOS Objective-C