iphone - 如何更改 UISearchbar 取消按钮的文本颜色

标签 iphone objective-c ios uisearchbar

我有一个 UISearchBar,如下所示。如何更改取消按钮的文本颜色?

enter image description here

最佳答案

这个问题之前有人问过,所以我认为问的人已经找到了解决方案。但以防万一其他人碰巧遇到同样的问题。这是我的解决方案。

我有一个带有取消按钮的 UISearchBar,该按钮仅在点击 UISearchBar 的文本字段时出现。因此,在 UISearchBar 的子类中覆盖 -(void)layoutSubviews 的解决方案对我来说不是一个选择。不管怎样,我做了一个 UISearchBar (CustomSearchBar) 的子类,它有一个公共(public)方法来设置取消按钮的字体和 textColor。当我创建 UISearchBar 时,我确保搜索栏的文本字段委托(delegate)设置为 self 并且创建搜索栏的类实现了 UITextFieldDelegate 协议(protocol)。当用户点击搜索栏的文本字段时,其委托(delegate)会收到通知并调用 CustomSearchBar 的方法。我之所以在这里这样做,是因为这是取消按钮出现的时刻,因此我知道它在 View 层次结构中,我可以对其进行自定义。

代码如下:

在 MyRootViewController 中创建 UISearchBar

CustomSearchBar *searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40)];
[searchBar setBarStyle:UIBarStyleDefault];
[searchBar setTintColor:[UIColor whiteColor]];

for (UIView *view in [searchBar subviews]) 
{
    if ([view isKindOfClass:[UITextField class]]) 
    {
        UITextField *searchTextField = (UITextField *)view;
        [searchTextField setDelegate:self];
    }
}

self.searchBar = searchBar;   
[searchBar release];

MyRootViewController 中的 UITextFieldDelegate(确保它实现了 UITextFieldDelegate 协议(protocol))

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]];
}

这是 UISearchBar 子类中的公共(public)方法

- (void)setCloseButtonFont:(UIFont *)font textColor:(UIColor *)textColor
{
    UIButton *cancelButton = nil;

    for(UIView *subView in self.subviews)
    {
        if([subView isKindOfClass:[UIButton class]])
        {
            cancelButton = (UIButton*)subView;
        }
    }

    if (cancelButton)
    {
        /* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations      make this not possible:

        UILabel *titleLabel = [cancelButton titleLabel];
        [titleLabel setFont:font];
        [titleLabel setTextColor:[UIColor redColor]];*/

        // Therefore I had to create view with a label on top:        
        UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(2, 2, kCancelButtonWidth, kCancelButtonLabelHeight)];
        [overlay setBackgroundColor:[UIColor whiteColor]];
        [overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work
        [cancelButton addSubview:overlay];

        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, kCancelButtonWidth, kCancelButtonLabelHeight)];
        [newLabel setFont:font];
        [newLabel setTextColor:textColor];
        // Text "Cancel" should be localized for other languages
        [newLabel setText:@"Cancel"]; 
        [newLabel setTextAlignment:UITextAlignmentCenter];
        // This is important for the cancel button to work
        [newLabel setUserInteractionEnabled:NO]; 
        [overlay addSubview:newLabel];
        [newLabel release];
        [overlay release]; 
    }
}

关于iphone - 如何更改 UISearchbar 取消按钮的文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6992917/

相关文章:

ios - 使用变量设置 UISlider 的值

ios - 如何更新 MKPinAnnotationView 的信息?

iphone - 访问 CGImage 的来源?

iphone - 如何在 CoreData 中存储 DayOfWeek 和时间?

ios - sysctlbyname错误函数 'sysctlbyname'的隐式声明在C99中无效

iphone - NSUrl 变为 null 时有效的 URL 字符串

objective-c - UIViewController 中的 TableView

ios - 使用 NSFetchedResultsController 在 TableView 上显示的元素列表与底层 CoreData 实体不匹配

iphone - 在 Objective-C 中测试类的类型

ios - 如何仅为 64 位 iOS 设备提交存档?