iOS - 自定义UISearchBar的取消按钮

12
在我的iOS5 iPhone应用中,我使用以下代码设置搜索栏的色调颜色:
searchBar.tintColor = UIColorMake(@"#EFEFEF");

#efefef的RGB值为(239,239,239)。
目前一切正常,但当取消按钮出现时,"Cancel"文本不可见。我能否只自定义取消按钮,使其具有透明的黑色和白色文本?
请问是否可以进行自定义?


我已经在这个主题下写了一个答案:https://dev59.com/YGIk5IYBdhLWcg3wgub-。只需使用SHSearchBar,它不像UISearchBar那样让人头疼。 - blackjacx
4个回答

42

您可以使用外观代理自定义iOS 5上的取消按钮。 当包含在UISearchBar中时,您需要更改UIBarButtonItem的外观。 例如,要更改取消按钮的标题字体,可以使用以下代码:

NSDictionary *attributes =
    [NSDictionary dictionaryWithObjectsAndKeys:
     [UIColor whiteColor], UITextAttributeTextColor,
     [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5], UITextAttributeTextShadowColor,
     [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
     [UIFont systemFontOfSize:12], UITextAttributeFont,
     nil];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
    setTitleTextAttributes:attributes forState:UIControlStateNormal];
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil]
    setTitleTextAttributes:attributes forState:UIControlStateHighlighted];

5
这应该是被接受的答案。它并没有什么技巧性,很清晰,而且有效。目前被接受的答案可能随时会出问题。 - csotiriou
1
这对于更改按钮上的文本非常好,但是如何更改按钮本身呢?例如-更改按钮大小。 - Zayin Krige
1
不幸的是,UISearchBar还包含一个Clear按钮(灰色圆圈内有一个x),因此如果使用此方法更改取消按钮的背景图像,则Clear按钮也会受到影响... - Louis Nguyen

5
你可以搜索 UISearchBar 的子视图并定位取消按钮,但这样做很危险,因为该按钮可能会更改。例如,你可以在你的 viewWillAppear 方法中添加以下内容:
- (void) viewWillAppear:(BOOL)animated
{
    //show the cancel button in your search bar
    searchBar.showsCancelButton = YES;
    //Iterate the searchbar sub views
    for (UIView *subView in searchBar.subviews) {
        //Find the button
        if([subView isKindOfClass:[UIButton class]])
        {
            //Change its properties
            UIButton *cancelButton = (UIButton *)[sb.subviews lastObject];
            cancelButton.titleLabel.text = @"Changed";
        }
    }
}

正如我之前所说,这可能会改变,这是一种hack的方式,你最好坚持原来的方式,或者创建自己的搜索栏。


2
在iOS 7中它找不到任何子视图。 - NSCry
这并不可取,而自iOS 5以来,更好的处理方式是使用外观代理,就像Marián Černý在他的回答中所描述的那样。 - Craig B

4

自从iOS5以来,您可以使用以下代码编辑导航栏、工具栏、选项卡栏等等...

NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIColor darkGrayColor], 
                                          UITextAttributeTextColor, 
                                          [UIColor whiteColor], 
                                          UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];

我还没有用搜索栏测试过,但它应该类似地工作。


不,它没有。如果您查看搜索栏的外观文档,它并没有提供清晰的处理程序来更改搜索按钮的属性。 - csotiriou

0

这个方法适用于IOS7

for (UIView *view in searchBar.subviews)
    {
        for (id subview in view.subviews)
        {
            if ( [subview isKindOfClass:[UIButton class]] )
            {
                // customize cancel button
                UIButton* cancelBtn = (UIButton*)subview;
                [cancelBtn setEnabled:YES];
                break;
            }
        }
    }

请检查此链接:https://dev59.com/2Wkw5IYBdhLWcg3wVpBi#18150826

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接