UISearchBar increases navigation bar height in iOS 11

后端 未结 19 1453
醉酒成梦
醉酒成梦 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:08

    All you have to do is to subclass UISearchBar and override "intrinsicContentSize":

    @implementation CJSearchBar
    -(CGSize)intrinsicContentSize{
        CGSize s = [super intrinsicContentSize];
        s.height = 44;
        return s;
    }
    @end
    

    0 讨论(0)
  • 2020-11-28 03:09

    try this code on "ACKNOWLEDGEMENTS" view controller in viewDidLoad

    self.extendedLayoutIncludesOpaqueBars = true
    
    0 讨论(0)
  • 2020-11-28 03:10

    I got black line under NavigationBar with SearchBar in iOS 11 in two cases:

    • when i pushed another ViewControllers from ViewController with UISearchBar

    • when i dismissed ViewController with UISearchBar with "drag right to dismiss"

    My solution was: adding this code to my ViewController with UISearchBar:

    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
        [self.navigationController.view setNeedsLayout]; // force update layout
        [self.navigationController.view layoutIfNeeded]; // to fix height of the navigation bar
    }
    

    Swift 4 Update

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.view.setNeedsLayout() // force update layout
        navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
    }
    
    0 讨论(0)
  • 2020-11-28 03:10

    Unable to comment, but wanted to share some additional issues I ran into while spending many hours trying to get to the bottom of this issue even after using one of the other solutions.

    It appears the best fix for me was Andrew's answer:

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.view.setNeedsLayout() // force update layout
        navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
    }
    

    However, at the very least in iOS 12.1, if your UINavigationBar:

    • has isTranslucent set to false, the View Controller with the search bar appears to not get it's view's layout adjusted back when interactively dismissing (normal dismissing via back button appears to work).
    • has it's background image set using setBackgroundImage(UIImage(), for: .default), the transition animation doesn't work properly and will jump back to its position after finishing.

    These particular properties were set to get the Navigation Bar to appear in a certain way however, so I need to do some adjusting to get it back, or put up with the weird behaviour. Will try to remember to update the above if I run into anything else or find other solutions or differences in other OS versions.

    0 讨论(0)
  • 2020-11-28 03:13

    Thank you all! I finally found a solution.

    Adding the following code to ViewController with UISearchBar.

    1. First step: viewDidLoad
    -(void)viewDidLoad
    {
        [super viewDidLoad];
        self.extendedLayoutIncludesOpaqueBars = YES;
        ...
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.extendedLayoutIncludesOpaqueBars = true
    }
    
    1. Second step:viewWillDisappear
    -(void)viewWillDisappear:(BOOL)animated{
        [super viewWillDisappear:animated];
         // force update layout
        [self.navigationController.view setNeedsLayout]; 
        // to fix height of the navigation bar
        [self.navigationController.view layoutIfNeeded];  
    }
    
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            navigationController?.view.setNeedsLayout() // force update layout
            navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
        }
    
    0 讨论(0)
  • 2020-11-28 03:15

    I fixed this by added the constraint to viewDidAppear on the map view controller where the search bar is embedded

    public override func viewDidAppear(_ animated: Bool) {
        if #available(iOS 11.0, *) {
    
            resultSearchController?.searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
            // searchBar.heightAnchor.constraint(equalToConstant: 44).isActive = true
        }
    }
    
    0 讨论(0)
提交回复
热议问题