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#>)

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to help you..:)
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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.