ios - 如何使用 swift 设置条形按钮的图像?

标签 ios swift uiimage uibarbuttonitem

我正在尝试为条形按钮项目设置一个图像,因为我有一个像这样的图像:

enter image description here

分辨率为 30 * 30,但当我将此图像分配给栏按钮时,它看起来像:

enter image description here

我以这种方式分配图像:

enter image description here

如果我尝试这种方式,比如为按钮制作一个 IBOutlet 并以编程方式设置图像形式 this问题和代码是:

 // Outlet for bar button
 @IBOutlet weak var fbButton: UIBarButtonItem!

// Set Image for bar button
var backImg: UIImage = UIImage(named: "fb.png")!
fbButton.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

但这并没有发生什么,

谁能告诉我我做错了什么?

或者哪种方法更有效?

最佳答案

我已经用这段代码以编程方式实现了这一点:

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")
    }
}

结果将是:

enter image description here

你也可以用这种方式设置左侧的按钮:

self.navigationItem.leftBarButtonItem = barButton

结果将是:

enter image description here

如果您希望在使用默认后退按钮返回时与导航 Controller 具有相同的事务,那么您可以使用以下代码通过自定义后退按钮实现:

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

对于 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")
    }
}

对于 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")
    }
}

关于ios - 如何使用 swift 设置条形按钮的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27499998/

相关文章:

swift - NSAttributedString:图像未显示在 UILabel 中

ios - 在 UIImage 周围添加边框

ios - 如何在自定义单元格中选择 TextView 时同步事件?

ios - 如何更改 UINavigationBar 的字体和颜色?

ios - Swift:如何在 View Controller 之间切换并使用导航栏在 subview Controller 内向后移动(XLPagerTabStrip)

ios - 在 UITextView 中获取选定的文本

iOS - 在一个 TableViewController 下有 2 个表格 View 和其他元素

iOS:如何制作一个像画东西一样的画板?

ios - 如何使用新 API willAnimateRotationToInterfaceOrientation 修复 iOS 方向

ios - 在 UICollectionViewCell 中获取图像