UISearchBar increases navigation bar height in iOS 11

后端 未结 19 1591
醉酒成梦
醉酒成梦 2020-11-28 02:47

I have my UISearchBar being part of the navigation bar like:

 let searchBar = UISearchBar()
 //some more configuration to the search bar
 .....
         


        
19条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 03:01

    I couldn't use the solution of keeping the navBar at 44. So it took me a day but finally, I found a solution that doesn't change the bar height and position the button in the middle of the bar. The issue is that the buttons are placed in a stack view which is configured as Horizontal stack view and therefore doesn't adjust to the height change.

    This is done on init:

    UIBarButtonItem *cancelButton;
    if (@available(iOS 11.0, *)) {
        // For iOS11 creating custom button to accomadate the change of navbar + search bar being 56 points
        self.navBarCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.navBarCustomButton setTitle:@"Cancel"];
        [self.navBarCustomButton addTarget:self action:@selector(cancelButtonTapped) forControlEvents:UIControlEventTouchUpInside];
        cancelButton = [[UIBarButtonItem alloc] initWithCustomView:self.navBarCustomButton];
    } else {
        cancelButton = [[UIBarButtonItem alloc] initWithTitle:MagicLocalizedString(@"button.cancel", @"Cancel")
                                                                                             style:UIBarButtonItemStylePlain
                                                                                            target:self
                                                                                            action:@selector(cancelButtonTapped)];
    }
    

    on viewWillApear (or anytime after the view was added to the navigation stack)

       if (@available(iOS 11.0, *)) {
            UIView *buttonsStackView = [navigationController.navigationBar subviewOfClass:[UIStackView class]];
            if (buttonsStackView ) {
                [buttonsStackView.centerYAnchor constraintEqualToAnchor:navigationController.navigationBar.centerYAnchor].active = YES;
                [self.navBarCustomButton.heightAnchor constraintEqualToAnchor:buttonsStackView.heightAnchor];
            }
        }
    

    And subviewOfClass is a category on UIView:

    - (__kindof UIView *)subviewOfClass:(Class)targetClass {
         // base case
         if ([self isKindOfClass:targetClass]) {
            return self;
         }
    
         // recursive
        for (UIView *subview in self.subviews) {
            UIView *dfsResult = [subview subviewOfClass:targetClass];
    
            if (dfsResult) {
               return dfsResult;
           }
       }
       return nil;
    }
    

提交回复
热议问题