如何从 UISearchDisplayController 以编程方式显示/隐藏此不透明视图?
可能在searchDisplayControllerWillBeginSearch
或searchDisplayControllerDidBeginSearch
我需要设置一些内容......但是什么?
感谢。
答案 0 :(得分:9)
UIKeyboardWillAppearNotification
暂时解决了。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
OpaqueView是 UIControl ,alpha = 0.8。
- (void) keyboardWillShow {
for( UIView *subview in self.view.subviews ) {
if( [subview isKindOfClass:[UIControl class]] ) {
UIControl *v = (UIControl*)subview;
if (v.alpha < 1) {
v.hidden = YES;
}
}
}
}
我使用这种 ORRIBLE方式临时修复问题....任何其他想法将不胜感激!
感谢。
答案 1 :(得分:2)
elpsk提供的代码是最新的,但不适用于 iOS7及以上
在iOS6和iOS7中运行的代码如下
- 在viewDidLoad中添加以下通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];
写下面的功能
- (void) keyboardWillShow {
for( UIView *subview in self.view.subviews ) {
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:@"UISearchDisplayControllerContainerView"])) {
UIControl *v = (UIControl*)subview;
if (v.alpha < 1) {
v.hidden = YES;
}
}
}
}
注意:代码只有一个额外条件,如在iOS7 UIControl类中成为UISearchDisplayControllerContainerView,
答案 2 :(得分:2)
另一个回答不适合我的地方。这个适用于iOS7和iOS8。
for( UIView *subview in self.view.subviews ) {
if([subview isMemberOfClass:[UIControl class]] ||
([[[subview class] description] isEqualToString:@"UISearchDisplayControllerContainerView"])) {
for(UIView *subView2 in subview.subviews)
{
for(UIView *subView3 in subView2.subviews)
{
if (subView3.alpha < 1) {
subView3.hidden = YES;
}
}
}
}
}
如果您不需要iOS7支持,请不要再使用searchDisplayController,因为它已被弃用。对于iOS8,请使用UISearchController和dimsBackgroundDuringPresentation 属性
答案 3 :(得分:0)
#pragma mark UISearchBarDelegate
// Displays a view to simulate the lose of focus
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = NO;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
UIButton *view1 = [[UIButton alloc] init];
view1.frame = CGRectMake(0, 0, 320, MAX(480, self.tableView.contentSize.height));
view1.alpha = 0.6;
view1.tag = 2000;
view1.backgroundColor = [UIColor blackColor];
[view1 addTarget:self
action:@selector(removeView)
forControlEvents:UIControlEventTouchUpInside];
[self.tableView setScrollEnabled:NO];
[self.tableView addSubview:view1];
[view1 release];
}
/**
* Pop the view and the keyboard
*/
- (void)removeView {
UIView *v = [self.tableView viewWithTag:2000];
v.hidden = YES;
[v removeFromSuperview];
[self.tableView setScrollEnabled:YES];
[self.searchBar resignFirstResponder];
}
当你写作时会显示该视图,所以我猜你应该在searchBarTextDidBeginEditing上使用它。如果我错了,在你开始搜索或其他什么时使用它。