【问题标题】:iOS - Customizing Cancel button of UISearchBariOS - 自定义 UISearchBar 的取消按钮
【发布时间】:2012-06-16 12:42:28
【问题描述】:

在我的 iOS5 iPhone 应用程序中,我正在使用以下代码设置搜索栏的色调:

searchBar.tintColor = UIColorMake(@"#EFEFEF");

#efefef 的 RGB 值为 (239,239,239)
它工作正常。但是当取消按钮出现时,文本“取消”是不可见的。我可以只自定义带有透明黑白文本的取消按钮吗?
可以定制吗?

【问题讨论】:

标签: ios uisearchbar cancel-button


【解决方案1】:

您可以使用外观代理自定义 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];

【讨论】:

  • 这应该是公认的答案。这不是hacky,很清楚,而且有效。当前接受的答案可能随时中断。
  • 这对于更改按钮上的文本非常有用,但是如何更改按钮本身呢?例如 - 更改按钮大小
  • 不幸的是 UISearchBar 还包含一个清除按钮(内部带有 x 的灰色圆圈),因此如果您使用此方法更改取消按钮的背景图像,清除按钮也会受到影响...
【解决方案2】:

您可以搜索UISearchBar subViews 并找到取消按钮,这样做很危险,因为按钮可能会更改 例如,您可以将其添加到您的 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";
        }
    }
}

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

【讨论】:

  • 在 ios 7 中找不到任何子视图
  • 这是不可取的,从 iOS 5 开始,处理此问题的更好方法是使用外观代理,正如 Marián Černý 在他的回答中所描述的那样。
【解决方案3】:

从 iOS5 开始,您可以使用此代码编辑导航栏、工具栏、标签栏等...

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

我还没有用搜索栏测试过它,但它应该可以工作。

【讨论】:

  • 不,它没有。如果您查看搜索栏的外观文档,它并没有为更改搜索按钮的属性提供明确的处理程序。
【解决方案4】:

此方法适用于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://stackoverflow.com/a/18150826/1767686

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多