UISearchDisplayDelegate how to remove this opaque

2020-03-31 07:11发布

问题:

how can i programmatically show/hide this opaque view from UISearchDisplayController?

Probably in searchDisplayControllerWillBeginSearch or searchDisplayControllerDidBeginSearch i need to set something... but what?

thanks.

回答1:

Temporary solved using UIKeyboardWillAppearNotification.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];

OpaqueView is an UIControl with 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;
      }
    }
  }
}

I used this ORRIBLE way to temporary fix problem.... any other idea will be appreciated!

thanks.



回答2:

Code given by elpsk is current but will not work in iOS7 and above
Code working in both iOS6 and iOS7 is as below
- add below notification in viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) name:UIKeyboardWillShowNotification object:nil];


Write below function

- (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;
          }
        }
      }
    }

NOTE : Code just have one extra condition as in iOS7 UIControl class become UISearchDisplayControllerContainerView,



回答3:

The other answers where not working for me. This one works for me on iOS7 and 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;
                }
            }
        }
    }
}

If you don't need support for iOS7 please don't use the searchDisplayController anymore because its deprecated. For iOS8 use the UISearchController and the dimsBackgroundDuringPresentation Property

Ref: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UISearchController/index.html#//apple_ref/occ/instp/UISearchController/dimsBackgroundDuringPresentation



回答4:

Mmmm...quick answer. Not pretty but surely works

    #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];
}

That view is showed when you're writing, so I guess you should use it at searchBarTextDidBeginEditing. If I'm wrong, use it when you start searching or whatever.