在我的应用程序中,我必须在表格视图的顶部添加一个搜索栏。我可以添加搜索栏,但问题是没有添加 ios 的默认搜索栏我可以添加我的自定义搜索栏吗?我正在提供一张图片,看看会有哪些类型的搜索栏......
问问题
10725 次
5 回答
9
您可以子类化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;
}
}
}
于 2011-12-28T11:08:02.737 回答
0
是的,绝对的。您可以制作自定义搜索栏(它是 的子类UIView
)并将其作为子视图添加到tableHeaderView
.
于 2011-12-28T11:08:03.073 回答
0
[[searchBarDesign.subviews objectAtIndex:0] removeFromSuperview];
这里 searchBarDesign 是我的 searchBar 名称。
于 2012-11-20T07:06:58.473 回答
0
我认为最好在加载时设置 UISearchBar 的所有属性。
@interface MySearchBar : UISearchBar
@end
@implementation MySearchBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self myInitialize];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self myInitialize];
}
-(void)myInitialize
{
self.backgroundImage = [UIImage imageNamed:@"image.png"];
for (UIView* subview in self.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
//customize text field
UITextField* textfield = (UITextField*) subview;
}
}
}
@end
于 2013-08-24T01:37:41.877 回答