1

I followed along here to get a bigger navigation bar working. The result is that it looks bigger, however not all elements of the bar actually expand, meaning that I can only interact with items places in the original size of the navigation bar. This is my pain point because I am trying to expand the UINavigationBar to put buttons in the expanded area and these can't be pressed.

Here's my code:

@IBDesignable
class CustomNavigationBar: UINavigationBar {

    var customHeight: CGFloat = 88

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        let button = UIButton(frame: CGRect(x: 0, y: 44, width: 100, height: 44))
        button.setTitle("Button", for: .normal)
        button.backgroundColor = .red
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        self.addSubview(button)    
    }

    @objc func buttonAction() {
        print("button pressed")
    }

    override func sizeThatFits(_ size: CGSize) -> CGSize {
        return CGSize(width: UIScreen.main.bounds.width, height: customHeight)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        for subview in self.subviews {
            var stringFromClass = NSStringFromClass(subview.classForCoder)
            if stringFromClass.contains("UIBarBackground") {
                subview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: customHeight)
                subview.backgroundColor = .green
                subview.sizeToFit()
            }
            stringFromClass = NSStringFromClass(subview.classForCoder)
            if stringFromClass.contains("UINavigationBarContentView") {
                subview.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: customHeight)
                subview.backgroundColor = .black
                subview.sizeToFit()
            }
        }
    }
}

Here's the screenshot from that output:

enter image description here

You can see here that when I resized the UIBarBackground in the layoutSubviews() function, I made it green and set it to the custom height, which worked. However when I resized the UINavigationBarContentView and set its colour to black, the colour gets set fine, but the height stays at 44.

See a screen shot of the debug view hierarchy below and we see that there are actually two items that still have the original height of 44.

enter image description here

All of this means that I cannot press the button in the view at all. However, if I move it up a bit so that it is inside the 44 height, then I can press it.

Looking for some help as to how I can properly resize all aspects of this nav bar

EDIT

If I remove the subview.sizeToFit() from the UInavigationBarContentView it resizes it to the correct size. I also noticed that the overridden sizeThatFits is never called?

Shivam Tripathi
  • 1,405
  • 3
  • 19
  • 37
Mr.P
  • 1,390
  • 13
  • 35

1 Answers1

0

i was try to change the height before but also i couldn't ,but i makes the custom navigation controller and am call it on the viewDidLoad and hide the native navigation controller by doing

self.navigationController?.navigationBar.isHidden = true

and i display the custom in the same place and i put my views on this navigation controller also you can do animations on height this custom nav bar if you want

i hope this help you

Dark Knight
  • 370
  • 3
  • 13
  • This doesn't help, setting it to not be hidden isn't the issue. Also it shouldn't matter if I'm using a nav controller or just the bar. – Mr.P Apr 26 '18 at 09:40