我想更改UISearchBar
的外观:
是否有办法使UITextField
(在自定义背景下进行搜索)像UISearchBar
一样起作用?还是子类化并覆盖- (void)layoutSubviews
是唯一方法?
请告诉我们如何继承它!
最佳答案
您可以使用此波纹管代码来更改UISearchBar
背景。
for (UIView *subview in searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SkyBackground.jpeg"]];
[searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
break;
}
}
通过此代码,您可以为
UISearchBar
的背景设置图像或其他任何东西。另请参见我的此答案以及其他功能,以更改搜索栏组件的布局。How to change inside background color of UISearchBar component on iOS
更新:
要更改“取消”按钮的外观,请使用下面的代码...
UIButton *cancelButton = nil;
for(UIView *subView in searchBar.subviews)
{
if([subView isKindOfClass:[UIButton class]])
{
cancelButton = (UIButton*)subView;
//do something here with this cancel Button
}
}
关于iphone - 使UItextfield像UISearchBar一样起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14134443/