簡體   English   中英

如何更改UISearchbar取消按鈕的文本顏色

[英]How do I change the UISearchbar cancel button's text color

我有一個UISearchBar,如下所示。 如何更改取消按鈕的文本顏色?

在此輸入圖像描述

剛才提出這個問題,因此我認為提問的人已經找到了解決方案。 但是為了防止其他一些碰巧遇到同樣的問題。 這是我的解決方案。

我有一個帶有取消按鈕的UISearchBar,只有在點擊UISearchBar的文本字段時才會出現。 因此,在UISearchBar的子類中覆蓋 - (void)layoutSubviews的解決方案對我來說不是一個選項。 無論如何,我做了一個UISearchBar(CustomSearchBar)的子類,使用了一個公共方法來設置取消按鈕的字體和textColor。 當我創建UISearchBar時,我確保搜索欄的textfield委托設置為self,並且創建搜索欄的類實現UITextFieldDelegate協議。 當用戶點擊搜索欄的文本字段時,會通知其委托並調用CustomSearchBar的方法。 我在這里做的原因是因為它是取消按鈕出現的時刻,因此我知道它在視圖層次結構上,我可以進行自定義。

這是代碼:

用於在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協議)

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

這是UISearchBar子類中的公共方法

- (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]; 
    }
}

而不是做所有這些花哨的事情只是像這樣實現SearchBarTextDidBeginEditing

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    // only show the status bar’s cancel button while in edit mode sbar (UISearchBar)
    searchBar.showsCancelButton = YES;
    searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    UIColor *desiredColor = [UIColor colorWithRed:212.0/255.0 green:237.0/255.0 blue:187.0/255.0 alpha:1.0];


    for (UIView *subView in searchBar.subviews){
        if([subView isKindOfClass:[UIButton class]]){
            NSLog(@"this is button type");

            [(UIButton *)subView setTintColor:desiredColor];
            [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
}

Gyanerdra的答案很有效。 但對於iOS7,我需要在我的應用程序中進行以下更改。

NSArray *childViews;
if ( (APP).isIOS7 ) {
    childViews = [[searchBar.subviews objectAtIndex:0] subviews];
} else {
    childViews =searchBar.subviews;
}

for (UIView *subView in childViews ) {
    if([subView isKindOfClass:[UIButton class]]){
        [(UIButton *)subView setTintColor:desiredColor];
        [(UIButton *)subView setTitleColor:desiredColor forState:UIControlStateNormal];
    }
}

似乎對於iOS7,搜索欄包含在父視圖中。 希望這有助於某人。 b

您可以繼承UISearchBar並編寫自己的- (void)layoutSubviews方法。 在此方法中,循環遍歷其子視圖並獲取cancelButton。 其余的應該是直截了當的。

您可以利用iOS運行時屬性_cancelButton來實現此目的。

UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];

更改文本后,無法更改UISearchBar取消按鈕標題顏色。

KVC

UIButton * button = [_ searchBar valueForKey:@“_ cancelButton”]; button.titleLabel.font = [UIFont systemFontOfSize:13];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM