UISearchBar外观和Delegate

阅读更多

初始化

    内部搜索栏高度不随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)