简体   繁体   English

导航栏按钮图像

[英]Navigation bar button image

I am using this code to get a logo on my nav bar.我正在使用此代码在我的导航栏上获取徽标。

override func viewDidAppear(animated: Bool) {

    let image = UIImage(named: "LogoWithTextSmaller.png")
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
    }

This is fine, but the logo doesn't have any colour - besides 'blue'.这很好,但徽标没有任何颜色 - 除了“蓝色”。 Is it because it is a png file.是不是因为它是一个png文件。 Is there something I can so it retains the original colours有什么我可以的,所以它保留了原来的颜色

I have done this:我已经这样做了:

self.navigationItem.titleView = UIImageView(image: image)

and that brings the image onto the nav bar with the correct colours - but it's in the middle and I want it on the left.这会将图像以正确的颜色带到导航栏上 - 但它在中间,我希望它在左边。

You need to declare that the image stays original all the time.您需要声明图像始终保持原始状态。 so add the code as below所以添加如下代码

var image = UIImage(named: "image-name")
image = image?.withRenderingMode(.alwaysOriginal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: nil)

In Swift 3 the same would be accomplished using the following syntaxSwift 3 中,同样可以使用以下语法完成

var image = UIImage(named: "Filter")
image = image?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image:image , style: UIBarButtonItemStyle.plain, target: nil, action: nil)

Swift 3.0斯威夫特 3.0

let btnLogo = UIButton(frame: CGRect(x: 0, y: 0, width: 25, height: 25))
btnLogo.setTitle("", for: .normal)
btnLogo.backgroundColor = UIColor.clear
btnLogo.layer.cornerRadius = 4.0
btnLogo.layer.masksToBounds = true

var imageLogo = UIImage(named: "LogoWithTextSmaller.png")
imageLogo = imageLogo?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
btnLogo.setImage(imageLogo, for: .normal)

let barButton = UIBarButtonItem(customView: btnLogo)
self.navigationItem.leftBarButtonItem = barButton

swift 2.0快速 2.0

var image = UIImage(named: "Filter")
image = image?.imageWithRenderingMode(UIImageRenderingMode.alwaysOriginal)

Objective-C目标-C

 UIImage *image = [[UIImage alloc] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

 UIBarButtonItem *_btnLeftBar = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"LogoWithTextSmaller.png"]
                                                      style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:@selector(yourMethod)];

 self.navigationItem.rightBarButtonItem= _btnLeftBar;

For withRenderingMode(_:) details see below apple documentation link https://developer.apple.com/documentation/uikit/uiimage/1624153-withrenderingmode有关 withRenderingMode(_:) 详细信息,请参阅下面的苹果文档链接https://developer.apple.com/documentation/uikit/uiimage/1624153-withrenderingmode

SWIFT 4快速 4

    let back = UIImage(named: "back_white")?.withRenderingMode(.alwaysOriginal)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: back, style:.plain, target: nil, action: nil)

In swift 3.0在快速 3.0

    let Navigateimage = UIImage(named: "LogoWithTextSmaller.png")
    Navigateimage = Navigateimage?.withRenderingMode(.alwaysOriginal)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: Navigateimage, style:.plain, target: nil, action: nil)

Swift 3 If the item is missing, you can try this. Swift 3如果物品丢失,你可以试试这个。

  let navigationBar = navigationController?.navigationBar
  let topItem = navigationBar?.topItem
  var navigateimage = UIImage(named: "addConnectionFromSupport")
  navigateimage = navigateimage?.withRenderingMode(.alwaysOriginal)
  topItem?.rightBarButtonItem = UIBarButtonItem(image: navigateimage, style:.plain, target: nil, action: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM