How to set image for bar button with swift?

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

    Here's a simple extension on UIBarButtonItem:

    extension UIBarButtonItem {
        class func itemWith(colorfulImage: UIImage?, target: AnyObject, action: Selector) -> UIBarButtonItem {
            let button = UIButton(type: .custom)
            button.setImage(colorfulImage, for: .normal)
            button.frame = CGRect(x: 0.0, y: 0.0, width: 44.0, height: 44.0)
            button.addTarget(target, action: action, for: .touchUpInside)
    
            let barButtonItem = UIBarButtonItem(customView: button)
            return barButtonItem
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:13

    Just choose Original image option when adding an image to assets in Xcode

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

    If you have set up your UIBarButtonItem with an image in the storyboard, one small hack to change the renderingMode is to add the following code to your viewDidLoad(). This way you don't have to resort to adding the entire button and image in code.

    if let navButton = self.navigationItem.leftBarButtonItem, let buttonImage = navButton.image {
        navButton.image = buttonImage.withRenderingMode(.alwaysOriginal)
    }
    
    0 讨论(0)
  • 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")
        }
    }
    
    0 讨论(0)
  • 2020-11-28 05:25

    Your problem is because of the way the icon has been made - it doesn't conform to Apple's custom tab bar icon specs:

    To design a custom bar icon, follow these guidelines:

    • Use pure white with appropriate alpha transparency.
    • Don’t include a drop shadow.
    • Use antialiasing.

    (From the guidelines.)

    Something that would be possible looks like this. You can find such icons on most free tab bar icon sites.

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

    Similar to the accepted solution, but you can replace the

    let button: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    

    with

    let button = UIButton()
    

    Here is the full solution, enjoy: (it's just a bit cleaner than the accepted solution)

    let button = UIButton()
    button.frame = CGRectMake(0, 0, 51, 31) //won't work if you don't set frame
    button.setImage(UIImage(named: "fb"), forState: .Normal)
    button.addTarget(self, action: Selector("fbButtonPressed"), forControlEvents: .TouchUpInside)
    
    let barButton = UIBarButtonItem()
    barButton.customView = button
    self.navigationItem.rightBarButtonItem = barButton
    
    0 讨论(0)
提交回复
热议问题