gpt4 book ai didi

iphone - 在 iPhone 应用程序开发中自定义搜索栏

转载 作者:可可西里 更新时间:2023-11-01 06:20:23 25 4
gpt4 key购买 nike

在我的应用程序中,我必须在表格 View 的头部添加一个搜索栏。我可以添加搜索栏,但问题是没有添加 ios 的默认搜索栏我可以添加我的自定义搜索栏吗?我正在提供一张图片,看看那里会有什么类型的搜索栏...

enter image description here

最佳答案

您可以子类化 UISearchBar 并覆盖 layoutSubviews 方法:

- (void)layoutSubviews {
UITextField *searchField;
NSUInteger numViews = [self.subviews count];
for(int i = 0; i < numViews; i++) {
if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) { //conform?
searchField = [self.subviews objectAtIndex:i];
}
}
if(!(searchField == nil)) {
searchField.textColor = [UIColor whiteColor];
[searchField setBackground: [UIImage imageNamed:@"yourImage.png"] ];
[searchField setBorderStyle:UITextBorderStyleNone];
}

[super layoutSubviews];
}

你也可以:

//to clear searchbar backgraound
- (void) clearSearchBarBg
{
for (UIView *subview in theSearchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[subview removeFromSuperview];
break;
}
}
}

//display showSearchButtonInitially in a keyboard
- (void)showSearchButtonInitially
{
UIView * subview;
NSArray * subviews = [theSearchBar subviews];

for(subview in subviews)
{
if( [subview isKindOfClass:[UITextField class]] )
{
NSLog(@"setEnablesReturnKeyAutomatically");
[((UITextField*)subview) setEnablesReturnKeyAutomatically:NO];
((UITextField*)subview).delegate=self;
[((UITextField*)subview) setEnabled:TRUE];
((UITextField*)subview).borderStyle = UITextBorderStyleNone;
break;
}
}
}

关于iphone - 在 iPhone 应用程序开发中自定义搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8654967/

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