我在视图控制器中显示了两个搜索栏。这很奇怪,因为我在另一个 vc 中有相同的确切代码并且它工作正常。(后台的搜索栏不应该在那里)
这是一个屏幕截图:
我添加了代表:<UISearchBarDelegate, UISearchDisplayDelegate>
然后在.h中:
@property (nonatomic,retain) IBOutlet UISearchBar *searchBar;
在他们中:
@synthesize searchBar;
在 viewDidLoad 中:
self.searchBar.frame = CGRectMake(204, 11, 107,44);
self.searchBar.delegate = self;
//customize the searchbar
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"];
[searchField setBackground:[UIImage imageNamed:@"search_panel.png"]];
[self.searchBar setTintColor:[UIColor redColor]];
UIImage *searchimg = [UIImage imageNamed:@"searchfield_bg.png"];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
UIView *bg = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:searchimg];
[self.searchBar insertSubview:bg aboveSubview:subview];
[subview removeFromSuperview];
break;
}
}
[self.view addSubview:self.searchBar];
就是这样。我与 Storyboard 视图控制器 xib 中的 searchBar 无关,它是在 viewDidLoad 方法中以编程方式添加的
谢谢你的帮助