Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何更改UISearchbar取消按钮';s文本颜色_Iphone_Objective C_Ios_Uisearchbar - Fatal编程技术网

Iphone 如何更改UISearchbar取消按钮';s文本颜色

Iphone 如何更改UISearchbar取消按钮';s文本颜色,iphone,objective-c,ios,uisearchbar,Iphone,Objective C,Ios,Uisearchbar,我有一个UISearchBar,如下所示。如何更改“取消”按钮的文本颜色 您可以将UISearchBar子类化,并编写自己的-(void)layoutSubviews方法。在这个方法中,循环遍历它的子视图并获得cancelButton。其余的应该是直截了当的。这个问题是不久前提出的,因此我认为提出问题的人已经找到了解决方案。但以防万一,其他人也会遇到同样的问题。这是我的解决办法 我有一个带有取消按钮的UISearchBar,它只有在点击UISearchBar的文本字段时才会出现。因此,在UISe

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


您可以将UISearchBar子类化,并编写自己的
-(void)layoutSubviews
方法。在这个方法中,循环遍历它的子视图并获得cancelButton。其余的应该是直截了当的。

这个问题是不久前提出的,因此我认为提出问题的人已经找到了解决方案。但以防万一,其他人也会遇到同样的问题。这是我的解决办法

我有一个带有取消按钮的UISearchBar,它只有在点击UISearchBar的文本字段时才会出现。因此,在UISearchBar的子类中重写-(void)LayoutSubView的解决方案对我来说不是一个选项。无论如何,我用一个公共方法创建了UISearchBar(CustomSearchBar)的子类,用于设置取消按钮的字体和文本颜色。创建UISearchBar时,我确保搜索栏的textfield委托设置为self,并且创建搜索栏的类实现UITextFieldDelegate协议。当用户点击搜索栏的textfield时,会通知其代理并调用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协议)

这是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]; 
    }
}

与其做所有这些花哨的事情,不如像这样实现searchbartextdebeginediting

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

贾内德拉的答案很有效。但对于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

您可以利用iOS运行时属性
\u cancelButton
来实现这一点

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

UIButton*button=[\u搜索栏值forkey:@“\u取消按钮”];
button.titleLabel.font=[UIFont systemFontOfSize:13]

谢谢你的回答。但是,我不太熟悉如何重写layoutSubView方法。您能为我提供指导吗?只有当UISearchBar的属性ShowScanCellButton设置为“是”时,覆盖-(void)布局子视图才有效。默认情况下,该选项设置为“否”,因此,仅当点击UISearchBar的文本字段时,才会显示“取消”按钮。在这种情况下,覆盖layoutsubview将没有任何效果。这太棒了。非常感谢。我用了另一种我不太满意的方法。但有了这一点,我现在可以重新审视这个问题了。谢谢获取cancelButton=nil,直到您无法启用搜索栏。showsCancelButton=Yes这似乎会影响字体大小,而不是颜色。因此,您可以更改字体,也可以更改标题颜色。[按钮设置标题颜色:[UIColor orangeColor]用于状态:uicontrol状态正常];
UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];