Usman Shahid
Usman Shahid

Reputation: 193

Begin editing of UISearchbar changes the UINavigationbar color

In viewcontroller, i have searchbar and tableview see screenshot

When i click searchbar it changes the navigationbar color to black I have changed the navigation bar color, but still it changes navigation bar color

Here is what happens when i click searchbar see screenshot

Here is the code:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    self.setTitle("Add users", showBackButton: false, navBGColor: .white, navTitleColor: .black)
}

func setTitle(_ titleStr: String = "", showBackButton: Bool = true, navBGColor: UIColor = UIColor.white, navTitleColor: UIColor = UIColor.black) {

    self.title = titleStr

    self.hideNavigationBar(false)
    self.showBackButton(showBackButton, navTitleColor == .black)

    self.changeNavBarAppearance(navBGColor, navTitleColor)
}
func changeNavBarAppearance(_ navColor: UIColor, _ titleColor: UIColor = UIColor.white) {

    let titleTextAttribute = [NSAttributedStringKey.font:R.Fonts.AppNavBarFont,
                              NSAttributedStringKey.foregroundColor : titleColor]

    self.navigationController?.navigationBar.tintColor = titleColor
    self.navigationController?.navigationBar.barTintColor = navColor
    self.navigationController?.navigationBar.isTranslucent = false
    self.navigationController?.navigationBar.titleTextAttributes = titleTextAttribute
    self.navigationController?.navigationBar.shadowImage = UIImage()
}

Upvotes: 1

Views: 162

Answers (1)

Fast Coderz
Fast Coderz

Reputation: 361

I had similar kind of problem when i was adding keyboard notification observer in a viewcontroller.

I had set the keyboardWillShow notification observer in the viewcontroller, then i pushed another viewcontroller on it without removing the keyboard notification observer.

When i clicked search bar, it sends the notification to previous viewcontroller on which this new viewcontroller is pushed, that was invoking the keyboardWillShow method and i was changing the navigation bar color.

I removed the notification observer for keyboard and issue was fixed

Upvotes: 1

Related Questions