如何用swift设置条形按钮的图像? [英] How to set image for bar button with swift?

查看:117
本文介绍了如何用swift设置条形按钮的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





分辨率为30 * 30但是我将此图像分配给Bar按钮它看起来像:





我已按这种方式分配图像:





如果我尝试这种方式就像为按钮创建一个IBOutlet并以编程方式设置图像



同样,您可以通过这种方式为左侧设置按钮:

  self.navigationItem .leftBarButtonItem = barButton 

结果将是:





如果您想要使用默认返回按钮返回时导航控制器具有相同的交易,那么您可以使用此代码实现自定义后退按钮:

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

对于swift 3.0:

  import UIKit 

class ViewController:UIViewController {

override func viewDidLoad(){
super .viewDidLoad()
//创建一个新按钮
let button = UIButton.init(type:.custom)
//为按钮设置图像
button.setImage(UIImage(命名:fb.png),用于:UIControlState.normal)
//为按钮添加函数
button.addTarget(self,action:#selector(ViewController.fbButtonPressed),用于:UIControlEvents.touchUpInside )
//设置框架
button.frame = CGRect(x:0,y:0,宽度:53,高度:51)

let barButton = UIBarButtonItem(customView:按钮)
//将按钮分配给导航栏
self.navigationItem.rightBarButtonItem = barButton
}

//当您按下按钮时,此方法将调用。
func fbButtonPressed(){

print(Share to fb)
}
}


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

with resolution 30 * 30 but while I assign this Image to Bar button Its looks like:

I have assigned image this way :

and If I try this way like making an IBOutlet for the button and set Image programatically form this question and code for that is:

 // 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)

but nothing happend with this,

Can anybody tell me what I am doing wrong?

or which is the batter way to do this?

解决方案

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

这篇关于如何用swift设置条形按钮的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆