How to add / use default icons to navigation bar

后端 未结 3 636
迷失自我
迷失自我 2020-12-31 07:42

I want to use some of default iOS icons i.e.

in navigation bar.
Basically I don\'t know how to call image of that item (directly from native library - I kno

相关标签:
3条回答
  • 2020-12-31 07:49

    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
    
    0 讨论(0)
  • 2020-12-31 08:02

    You can use UIBarButtonSystemItem this way:

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

    Result for leftBarButtonItem:

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

    navigationItem.rightBarButtonItem = button
    

    Result for rightBarButtonItem:

    0 讨论(0)
  • 2020-12-31 08:04

    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.

    0 讨论(0)
提交回复
热议问题