14

I want to use some of default iOS icons i.e.
enter image description here

in navigation bar.
Basically I don't know how to call image of that item (directly from native library - I know how to download it and place as custom image:)):

   var myButton:UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
   myButton.addTarget(self, action: "reload", forControlEvents: UIControlEvents.TouchUpInside)
   myButton.setImage(???, forState: <#UIControlState#>)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
pbaranski
  • 22,778
  • 19
  • 100
  • 117

3 Answers3

26

You can use UIBarButtonSystemItem this way:

let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Refresh, target: self, action: "someAction")
navigationItem.leftBarButtonItem = button

Result for leftBarButtonItem:

enter image description here

If you want to set it at right side you can use this code:

navigationItem.rightBarButtonItem = button

Result for rightBarButtonItem:

enter image description here

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
11

Swift: These are the most commonly used options:

To Use custom image with original colour:

let customImageBarBtn1 = UIBarButtonItem(
    UIImage(named: "someImage.png").withRenderingMode(.alwaysOriginal),
    style: .plain, target: self, action: #selector(handleClick))

To Use custom image with tint colour:

let customImageBarBtn2 = UIBarButtonItem(
    UIImage(named: "someImage.png").withRenderingMode(.alwaysTemplate),
    style: .plain, target: self, action: #selector(handleClick))

Or use system provided buttons:

let systemBarBtn = UIBarButtonItem(
    barButtonSystemItem: .search,
    target: self, action: #selector(handleClick))

Then add any one of these buttons to the navigationItem:

navigationItem.leftBarButtonItems = [customImageBarBtn1, customImageBarBtn2]
navigationItem.rightBarButtonItems = [systemBarBtn]
// OR you can use this if there's only one item.
navigationItem.rightBarButtonItem = systemBarBtn

For custom Images: As a starting size, 22ptx22pt images work well for the default iPhone Navigation Bar size.

Nitin Nain
  • 5,113
  • 1
  • 37
  • 51
1

In swift 4.3

let btnRefresh = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.refresh, target: self, action: #selector(targeted function to invoke))

   //If you want icon in left side
    navigationItem.leftBarButtonItem = btnRefresh

   //If you want icon in right side
    navigationItem.rightBarButtonItem = btnRefresh
Sanjay Mishra
  • 672
  • 7
  • 17