我有一个带有取消按钮的UISearchBar (它是使用-(void)setShowsCancelButton:animated显示的)。我像这样更改了搜索栏的tintColor,试图获得一个灰色的搜索栏:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
searchBar.tintColor = [UIColor colorWithWhite:0.8 alpha:1.0];下面是它现在的样子--注意cancel按钮也是灰色的:http://twitpic.com/c0hte
有没有一种方法可以单独设置取消按钮的颜色,使其看起来更像这样:http://twitpic.com/c0i6q
发布于 2010-10-03 23:27:38
尽管这可能与原始问题不完全相关,但在更大的意义上,该解决方案仍然适用于尝试自定义UISearchBar中的Cancel按钮。我想这会帮助其他被困在这种情况下的人。
我的情况是更改了cancel按钮的标题,但有一点不同,我不想在默认情况下显示cancel按钮,而是只想在用户进入搜索模式时显示它(通过在搜索文本字段内单击)。此时,我希望cancel按钮带有标题"Done“("Cancel”赋予我的屏幕不同的含义,因此进行了自定义)。
然而,以下是我所做的(caelavel和Arenim的解决方案的组合):
使用以下两种方法将UISearchBar子类化为MyUISearchBar:
-(void) setCloseButtonTitle: (NSString *) title forState: (UIControlState)state
{
[self setTitle: title forState: state forView:self];
}
-(void) setTitle: (NSString *) title forState: (UIControlState)state forView: (UIView *)view
{
UIButton *cancelButton = nil;
for(UIView *subView in view.subviews){
if([subView isKindOfClass:UIButton.class])
{
cancelButton = (UIButton*)subView;
}
else
{
[self setTitle:title forState:state forView:subView];
}
}
if (cancelButton)
[cancelButton setTitle:title forState:state];
}在使用此搜索栏的视图控制器中,以下代码负责显示cancel按钮并定制其标题:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
MyUISearchBar *sBar = (MyUISearchBar *)searchBar;
[sBar setShowsCancelButton:YES];
[sBar setCloseButtonTitle:@"Done" forState:UIControlStateNormal];
}奇怪的是,我不需要做任何事情来隐藏cancel按钮,因为当退出搜索模式时,它是默认隐藏的。
https://stackoverflow.com/questions/1200149
复制相似问题