基本上我想要实现的是让我的范围栏永远不会消失。
环境:IOS 7,情节提要,在视图控制器内我有一个“搜索栏和搜索显示控制器”和一个单独的表格视图(搜索栏不在表格内)
在视图 controller.h 中
@property (nonatomic, strong) IBOutlet UISearchBar *candySearchBar;
在视图控制器.m 中
@synthesize candySearchBar;
我尝试了什么:在自定义搜索栏类中
- (void) setShowsScopeBar:(BOOL) showsScopeBar
{
if ([self showsScopeBar] != showsScopeBar) {
[super invalidateIntrinsicContentSize];
}
[super setShowsScopeBar:showsScopeBar];
[super setShowsScopeBar: YES]; // always show!
NSLog(@"setShowsScopeBar searchbar");
NSLog(@"%hhd", showsScopeBar);
}
和
searchBarDidEndEditing
视图控制器中的相同内容,但随后
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[candySearchBar setShowsScopeBar:YES];
[candySearchBar sizeToFit];
}
我希望我的问题很清楚,我尝试了许多在互联网上发布的解决方案,其中大多数都在谈论 setshowsscopebar,但它似乎不起作用。setshowscopebar 中的日志输出为 1,但仍然没有显示 scopebar。
我仍然认为自己是代码新手,错误仍然可能是新手错误。
编辑:视图控制器中的另一段代码,如您所见,我正在盲目搜索:
-(void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
self.searchDisplayController.searchBar.showsScopeBar = YES;
controller.searchBar.showsScopeBar = TRUE;
controller.searchBar.frame = CGRectMake(0, 149, 768, 88);
UIButton *cancelButton;
UIView *topView = self.searchDisplayController.searchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
//Set the new title of the cancel button
[cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
[cancelButton setEnabled:YES];
controller.searchBar.showsScopeBar = YES;
//candySearchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"Flags", @"Listeners", @"Stations", nil];
}
NSLog(@"%@",NSStringFromCGRect(controller.searchBar.frame));
NSLog(@"%@",NSStringFromCGRect(controller.searchBar.bounds));
NSLog(@"%hhd@",controller.searchBar.hidden);
}