如何向UISearchBar添加自定义按钮

时间:2014-10-11 09:48:42

标签: objective-c ios7 uisearchbar

我正在尝试自定义搜索栏。 我需要在搜索栏视图中添加两个按钮。 我无法将搜索栏添加到导航栏,例如here 这就是我要做的事情:

CGRect searchFrame = CGRectMake(7, 225, 30, 30);
self.searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.searchButton setBackgroundImage:[UIImage imageNamed:@"magnifier"] forState:UIControlStateNormal];
[self.searchButton setBackgroundColor:[UIColor redColor]];
[self.searchButton setImage:[UIImage imageNamed:@"magnifier"] forState:UIControlStateNormal];
[self.searchButton setFrame:searchFrame];

UITextField *searchField = [self.textSearchBar valueForKey:@"_searchField"];
[searchField setBackgroundColor:[UIColor whiteColor]];
[searchField setLeftView:UITextFieldViewModeNever];
[searchField setBorderStyle:UITextBorderStyleRoundedRect];
searchField.frame = CGRectMake(10, 7, 200, 30);

[self.textSearchBar addSubview:self.searchButton];

[self.textSearchBar reloadInputViews];

但它不起作用。可以做到?如何添加到searchBar子视图?

0 个答案:

没有答案