gpt4 book ai didi

iphone - 使 UItextfield 的功能类似于 UISearchBar

转载 作者:可可西里 更新时间:2023-11-01 03:37:52 29 4
gpt4 key购买 nike

我想改变 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com