xcodeuinavigationcontrolleruinavigationbarnslayoutconstraintlarge-title

Xcode lock view to bouncing navigation bar


animation

Situation

I have a UIView that has a layout constraint to the bottom of the safe area. This is inside a UIViewController inside a UINavigationController. It works fine when the navigation bar is in-between the large title and the "regular" title. However when bouncing lower the UINavigationBar covers the custom view.

Question

How can I lock the position of a custom view to the bottom of a bouncing NavigationBar. Storyboard solution would be optimal, Swift solution would be sufficient.


Solution

  • You need to add menuView in navigationBar

    let menuView = UIView()
    menuView.backgroundColor = .red
    menuView.translatesAutoresizingMaskIntoConstraints = false
    
    self.navigationController?.navigationBar.addSubview(menuView)
    
    [menuView.leadingAnchor.constraint(equalTo: (self.navigationController?.navigationBar.leadingAnchor)!),
    menuView.topAnchor.constraint(equalTo: (self.navigationController?.navigationBar.bottomAnchor)!),
    menuView.trailingAnchor.constraint(equalTo: (self.navigationController?.navigationBar.trailingAnchor)!),
    menuView.heightAnchor.constraint(equalToConstant: 60)].forEach{ $0.isActive = true }
    

    Result

    enter image description here

    But you have to maintain contentInset of UITableView/UICollectionView/UIScrollView & Scroll indicator

    Suggestions

    Use Section Header of TableView/CollectinView in this type of situation.