How to set image for bar button with swift?

前端 未结 14 1203
一个人的身影
一个人的身影 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:19

    I have achieved that programatically with this code:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            //create a new button
            let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
            //set image for button
            button.setImage(UIImage(named: "fb.png"), forState: UIControlState.Normal)
            //add function for button
            button.addTarget(self, action: "fbButtonPressed", forControlEvents: UIControlEvents.TouchUpInside)
            //set frame
            button.frame = CGRectMake(0, 0, 53, 31)
    
            let barButton = UIBarButtonItem(customView: button)
            //assign button to navigationbar
            self.navigationItem.rightBarButtonItem = barButton
        }
    
        //This method will call when you press button.
        func fbButtonPressed() {
    
            println("Share to fb")
        }
    }
    

    And result will be:

    Same way you can set button for left side too this way:

    self.navigationItem.leftBarButtonItem = barButton
    

    And result will be:

    And if you want same transaction as navigation controller have when you go back with default back button then you can achieve that with custom back button with this code:

    func backButtonPressed(sender:UIButton) {
        navigationController?.popViewControllerAnimated(true)
    }
    

    For swift 3.0:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            //create a new button
            let button = UIButton.init(type: .custom)
            //set image for button
            button.setImage(UIImage(named: "fb.png"), for: UIControlState.normal)
            //add function for button
            button.addTarget(self, action: #selector(ViewController.fbButtonPressed), for: UIControlEvents.touchUpInside)
            //set frame
            button.frame = CGRect(x: 0, y: 0, width: 53, height: 51)
    
            let barButton = UIBarButtonItem(customView: button)
            //assign button to navigationbar
            self.navigationItem.rightBarButtonItem = barButton
        }
    
        //This method will call when you press button.
        func fbButtonPressed() {
    
            print("Share to fb")
        }
    }
    

    For swift 4.0:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            //create a new button
            let button = UIButton(type: .custom)
            //set image for button
            button.setImage(UIImage(named: "fb.png"), for: .normal)
            //add function for button
            button.addTarget(self, action: #selector(fbButtonPressed), for: .touchUpInside)
            //set frame
            button.frame = CGRect(x: 0, y: 0, width: 53, height: 51)
    
            let barButton = UIBarButtonItem(customView: button)
            //assign button to navigationbar
            self.navigationItem.rightBarButtonItem = barButton
        }
    
        //This method will call when you press button.
        @objc func fbButtonPressed() {
    
            print("Share to fb")
        }
    }
    

提交回复
热议问题