Saturday, April 11, 2015

Overview

The UINavigationController is widely used in iOS. We’re going to look at how to customize one’s UINavigationBar. You can find more information on both of these classes here.

Content

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // cache the bar
    var nav = self.navigationController?.navigationBar
    
    // Change the background color of the bar
    nav?.barTintColor = UIColor.blueColor()

    // Change the NavigationBar's back button color
    nav?.tintColor = UIColor.redColor()

    // Is the bar translucent
    nav?.translucent = true

    // Create an imageView to place instead of the NavigationBar title
    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    imageView.contentMode = .ScaleAspectFit

    // Put an image inside the imageView
    let image = UIImage(named: "MyImage")
    imageView.image = image

    // place it instead of the title
    self.navigationItem.titleView = imageView
}

You can find the properties of a UINavigationItem here.

You can find the properties of a UINavigationController here.


Random Posts