-1

I am wanting to change the background of the UI Navbar title in iOS. I want the final result to appear like the image below:

enter image description here

Please let me know if this is possible or if I will have to do a workaround.

Changing navigation bar color in Swift does not address the background color of the title text for the UINavigationBar

Community
  • 1
  • 1
Matt Butler
  • 476
  • 6
  • 21
  • Possible duplicate of [Changing navigation bar color in Swift](http://stackoverflow.com/questions/24687238/changing-navigation-bar-color-in-swift) – brimstone Jun 01 '16 at 02:02

3 Answers3

0

Your best bet is to use the titleView property of UINavigationBar. Try something like this

    // Create new label that will service as a title view
    let titleView = UILabel()
    /* customize label to heart's content */
    titleView.text = "TITLE HERE"
    // Set your custom background color
    titleView.backgroundColor = UIColor.grayColor()
    // Now set this is as the titleView of the navigation bar
    self.navigationItem.titleView = titleView

If you have a statically managed managed UINavigationBar (not a part of UINavigationController). Something like this should work:

    navBar.pushNavigationItem(self.navigationItem, animated: NO)
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
0

So basically you need to add a UIView to the navigationBarTitleView. Then add a label or UIImageView to the Uiview and Finaly add that view to the TitleView. So here I've added label to the titleView. Hope that this will gonna help you:-

  override func viewDidLoad() {
    super.viewDidLoad()
    let navBarView: UIView = UIView()
    navBarView.frame = CGRectMake(0, 0, 100.0, 21.0)

    let label: UILabel = UILabel()
    label.frame = CGRectMake(0, 0, 90.0, 21.0)
    label.text = "Your Title"
    label.textAlignment = .Center
    label.backgroundColor = UIColor.grayColor()
    navBarView.addSubview(label)
    self.navigationItem.titleView = navBarView
    // Do any additional setup after loading the view, typically from a nib.
}

So here is the output :-

enter image description here

Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
0
class SomeNavigationController: UINavigationController {

override func awakeFromNib() {
    super.awakeFromNib()

    self.navigationBar.topItem?.title = NavTitle
    }

}

I have referenced NavTitlefrom a constants swift class. I hope this helps!

amagain
  • 2,042
  • 2
  • 20
  • 36