DanisFabric / RainbowNavigation

An easy way to change backgroundColor of UINavigationBar when Push & Pop
MIT License
786 stars 85 forks source link

Background View overlaps Left/RightNavigationItems #10

Open EstebanVallejo opened 7 years ago

EstebanVallejo commented 7 years ago

Hello!

I'm having an issue when I'm coming back (pop) to a UIViewController that has left and/or right navigationItems. When I wire the RainbowNavigation to the UIViewController navigationController, set the "navigationBarInColor()", and pop back to that viewController (dragging or not), the colored background view overlaps the navigationItems (except the title or titleView).

override func viewDidLoad() {
        super.viewDidLoad()

        let button = UIBarButtonItem(title: "HELLO", style: .plain, target: nil, action: nil)
        self.navigationItem.setRightBarButton(button, animated: true)

        if let navigationController = navigationController {
            rainbowNavigation.wireTo(navigationController: navigationController)
        }
}

...

func navigationBarInColor() -> UIColor {
        return navColor
}

I have tested it in my own app as in the ExamleApp, and it happens on both. I have been able to debug it, and found that the navigatinoBar.subviews order is the problem, when poping back the ViewController, the layers are rearranged and the backgroundView place itself over the NavigationItems and behind the (TitleView or Title).

I was not able to fix it yet, has anyone encountered this problem too???

fwx commented 6 years ago

I know that's an old question, but I managed to fix this.

UINavigationBar+Rainbow.swift

at public var backgroundColor there's an insertSubview function, so I changed it to code below. Not sure how much this would work lol.

if #available(iOS 11, *) {
     navigationBar.subviews[0].insertSubview(navigationView!, at: 0)
} else {
      navigationBar.insertSubview(navigationView!, at: 0)
}