我有一个带有取消按钮的UISearchBar (它是使用-(void)setShowsCancelButton:animated显示的)。我像这样更改了搜索栏的tintColor,试图获得一个灰色的搜索栏:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
searchBar.tintColor = [UIColor colorWithWhite:0.8 alpha:1.0];下面是它现在的样子--注意cancel按钮也是灰色的:http://twitpic.com/c0hte
有没有一种方法可以单独设置取消按钮的颜色,使其看起来更像这样:http://twitpic.com/c0i6q
发布于 2009-07-29 15:00:19
你想要做的事情是相当艰难的。没有内置的钩子可以到达cancel按钮。
然而,有几个选择,如果你愿意吉米打开引擎盖。
首先,UISearchBar是一个UIView,Cancel按钮也是一个视图,正如您所预期的那样,它作为子视图添加到搜索栏中。
我做过一些实验,我可以告诉你,当按钮在屏幕上时,它的大小是48,30。
所以在viewWillAppear中,你可以这样做:
发布于 2012-04-11 23:00:39
您可以使用UIAppearance设置cancel按钮的样式,而不迭代UISearchBar的子视图,而是迭代the UIButton header does not currently have any methods annotated with UI_APPEARANCE_SELECTOR的子视图。
编辑:向下钻取子视图,直到得到cancel按钮
但在调用searchBar.setShowsCancelButton(true, animated: true)之前,这通常会返回nil。
extension UISearchBar {
var cancelButton : UIButton? {
if let view = self.subviews.first {
for subView in view.subviews {
if let cancelButton = subView as? UIButton {
return cancelButton
}
}
}
return nil
}
}发布于 2012-10-01 18:26:16
更改“取消”按钮的标题:
[[UIButton appearanceWhenContainedIn:[UISearchBar class], nil] setTitle:@"newTitle" forState:UIControlStateNormal];等同于的Swift:
let cancelButton = UIButton.appearance(whenContainedInInstancesOf: [UISearchBar.self])
cancelButton?.setTitle("cancel".localized, for: .normal)https://stackoverflow.com/questions/1200149
复制相似问题