Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

ios - Set custom images to the UIBarButtonItem but it doesn't show any image

I want to set custom images to the UIBarButtonItem but it only shows a rectangular box around and It doesn't show the actual image.

func setupBrowserToolbar() {
    let browser = UIToolbar(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: 30))
    //配置返回组件
    let path = NSBundle.mainBundle().pathForResource("back", ofType: "png")
    let urlstr = NSURL(fileURLWithPath: path!)
    let data = NSData(contentsOfURL: urlstr)
    let btnback = UIBarButtonItem(image: UIImage(data: data!), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("backClicked:"))
    //分割按钮1
    let btngrap1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    //前进按钮
    let btnforward = UIBarButtonItem(image: UIImage(named: "forward.png"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("forwardClicked:"))
    //分割按钮2
    let btngrap2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    //重新加载
    let btnreload = UIBarButtonItem(image: UIImage(named: "reload.png"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("reloadClicked:"))
    //分割按钮3
    let btngrap3 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    //停止加载
    let btnstop = UIBarButtonItem(image: UIImage(named:"stop.png"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("stopClicked:"))
    progress = UIProgressView((frame: CGRect(x: 0, y: 48, width: self.view.frame.width-50, height: 2)))
    progress.progress = 0
    browser.setItems([btnback, btngrap1, btnforward, btngrap2, btnreload, btngrap3, btnstop], animated: true)
    ptimer = NSTimer(timeInterval: 0.2, target: self, selector: Selector("loadProgress"), userInfo: nil, repeats: true)
    self.view.addSubview(browser)
}

my app screen shot

The above method is toolbar configuration but I don't know what is wrong.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thats because UIBarButtonItem image's default rendering mode always draw the image as a template image, ignoring its color information (UIImageRenderingModeAlwaysTemplate). Just create your image using UIImage's method imageWithRenderingMode always original.

UIImage(named: "yourImageName")!.withRenderingMode(.alwaysOriginal)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...