How to set image for bar button with swift?

前端 未结 14 1134
一个人的身影
一个人的身影 2020-11-28 04:22

I am trying to set an Image for bar button Item for that I have an image like:

\"enter

相关标签:
14条回答
  • 2020-11-28 05:03

    Only two Lines of code required for this

    Swift 3.0

    let closeButtonImage = UIImage(named: "ic_close_white")
            navigationItem.rightBarButtonItem = UIBarButtonItem(image: closeButtonImage, style: .plain, target: self, action:  #selector(ResetPasswordViewController.barButtonDidTap(_:)))
    
    func barButtonDidTap(_ sender: UIBarButtonItem) 
    {
    
    }
    
    0 讨论(0)
  • 2020-11-28 05:05

    I am using latest swift (2.1) and the answer (Dharmesh Kheni and jungledev) does not work for me. The image color was off (when setting in IB, it was blue and when setting directly in UIButton, it was black). It turns out I could create the same bar item with the following code:

    let barButton = UIBarButtonItem(image: UIImage(named: "menu"), landscapeImagePhone: nil, style: .Done, target: self, action: #selector(revealBackClicked))
    self.navigationItem.leftBarButtonItem = barButton
    
    0 讨论(0)
  • 2020-11-28 05:08

    Initialize barbuttonItem like following:

    let pauseButton = UIBarButtonItem(image: UIImage(named: "big"),
                                      style: .plain,
                                      target: self,
                                      action: #selector(PlaybackViewController.pause))
    
    0 讨论(0)
  • 2020-11-28 05:09

    An easy solution may be the following

    barButtonItem.image = UIImage(named: "image")
    

    then go to your Assets.xcassets select the image and go to the Attribute Inspector and select "Original Image" in Reder as option.

    0 讨论(0)
  • 2020-11-28 05:09

    You can use this code for multiple bar button with custom image:

    self.navigationItem.leftBarButtonItem = nil
    
    let button = UIButton(type: .custom)
    button.setImage(UIImage (named: "ChatTab"), for: .normal)
    button.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
    //button.addTarget(target, action: nil, for: .touchUpInside)
    let barButtonItem = UIBarButtonItem(customView: button)
    
    let button2 = UIButton(type: .custom)
    button2.setImage(UIImage (named: "ActivityTab"), for: .normal)
    button2.frame = CGRect(x: 0.0, y: 0.0, width: 35.0, height: 35.0)
    //button.addTarget(target, action: nil, for: .touchUpInside)
    
    let barButtonItem2 = UIBarButtonItem(customView: button2)
    self.navigationItem.rightBarButtonItems = [barButtonItem, barButtonItem2]
    

    Result will be this:

    0 讨论(0)
  • 2020-11-28 05:09

    SwiftUI

    .navigationBarItems modifier takes any view you want:

    struct ContentView: View {
         var body: some View {
            NavigationView {
                Text("SwiftUI")
                .navigationBarItems(leading:
                    HStack {
                        Image(systemName: "trash")
                        Text("Trash")
                    }
                )
            }
        }
    }
    

    Leading with text[1]

    .navigationBarItems(trailing: Image(systemName: "trash") )
    

    .navigationBarItems(leading: Image(systemName: "trash.fill"),
                        trailing: Image(systemName: "trash")
    )
    

    You can use a button for each if you need an action for each of them.

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