【问题标题】:Change Cancel Button BackgroundColor and Text of UISearchBar in iOS7更改iOS7中UISearchBar的取消按钮BackgroundColor和Text
【发布时间】:2013-10-10 10:03:15
【问题描述】:

我有以下代码可以更改 UISearchBar 中“取消”按钮的背景。

for( UIView *subview in self.searchBar.subviews ){
    if ([subview isKindOfClass:[UIButton class]]) {
        [(UIButton *)subview setEnabled:YES];
        [(UIButton *)subview setTitle:@"New Button Text" forState:UIControlStateNormal];
        ((UIButton *)subview).tintColor = [UIColor colorWithRed:4/255.0 green:119/255.0 blue:152/255.0 alpha:1.0];
    }
}

问题是这段代码在iOS7中不起作用! UISearchBar 中的视图结构发生了什么变化?

编辑:在这里测试,这是 UISearchBar 的新层次结构:

UISearchBar:
---UIView:
------UISearchBarBackground
------UISearchBarTextField
------UINavigationButton

问题是我无法测试if ([sub isKindOfClass:[UINavigationButton class]])。此行抛出编译错误:Use of undeclared identifier: UINavigationButton

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios7 uisearchbar


    【解决方案1】:

    [Edited]

    试试这个代码。

    如果要更改所有取消按钮,请添加AppDelegate

    [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                                          [UIColor redColor],
                                                                                                          UITextAttributeTextColor,
                                                                                                          [UIColor whiteColor],
                                                                                                          UITextAttributeTextShadowColor,
                                                                                                          [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                                                          UITextAttributeTextShadowOffset,
                                                                                                          nil]
                                                                                                forState:UIControlStateNormal];
    

    在iOS7中,我们需要添加objectAtIndex:0 and subviews.

    iOS7 中的 searchBar 子视图层次结构已更改,请尝试以下操作:

    in iOS7:
    
    NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
    
    
    iOS6 and before:
    
    NSArray *searchBarSubViews =  self.searchBar.subviews;
    

    【讨论】:

    • 我编辑了我的答案,请重新检查。它在我的项目中运行良好。
    • 这对我也不起作用。 @Yahiko,这在 iOS 7.0.3 中仍然有效吗?迭代子视图时,我只看到 2 个元素;没有 UIBarButtonItems 或 UIButtons。困惑。
    【解决方案2】:

    解决方案:我创建了自己的按钮。这样我就可以管理所有属性,而无需发现 Apple 对元素做了什么

    我只需要创建一个UIView aux 来包含搜索栏和新按钮。

    self.searchBar.showsCancelButton = NO; //don`t show the original cancelButton
    
    self.cancelSearchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelSearchButton.frame = CGRectMake(250, 6, 60, 31);
    [self.cancelSearchButton setTitle:@"Cancelar" forState:UIControlStateNormal];
    [self.cancelSearchButton addTarget:self action:@selector(searchBarCancelButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    NSString *fontName = [[self.cancelSearchButton titleLabel] font].fontName;
    [[self.cancelSearchButton titleLabel] setFont:[UIFont fontWithName:fontName size:11.0]];
    UIView *aux = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    aux.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0];
    aux.layer.borderWidth = 1.0f;
    aux.layer.borderColor = [UIColor lightGrayColor].CGColor;
    aux.layer.cornerRadius = 0.0f;
    [aux addSubview:self.searchBar];
    [aux addSubview:self.cancelSearchButton];
    
    - (void)searchBarCancelButtonClicked{
        //do whatever I want to do here
    }
    

    【讨论】:

    • 这种方法的问题是默认情况下你不会得到漂亮的默认动画......或者如果你能,我很想知道如何。
    【解决方案3】:

    您可以利用 iOS 运行时属性 _cancelButton 来实现此目的。

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

    用于更改文本

    [self.searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多