【问题标题】:How to change UINavigationBar item image?如何更改 UINavigationBar 项目图像?
【发布时间】:2016-03-10 14:26:47
【问题描述】:

如何在单击导航栏项目图像时更改它?我正在尝试这个:

self.navigationItem.rightBarButtonItem?.image = UIImage(named: "bookmark")

但它不会更改/替换项目的图像。我做错了什么?

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationitem


    【解决方案1】:

    您需要创建一个 UIBarButtonItem 并设置它的图像。

    例如

    self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "imageName"), style: .Plain, target: self, action: "yourActionOrNil")
    

    【讨论】:

      【解决方案2】:
      let infoImage = UIImage(named: "my-icon")    
      let imgWidth = infoImage?.size.width    
      let imgHeight = infoImage?.size.height    
      let button:UIButton = UIButton(frame: CGRect(x: 0,y: 0,width: imgWidth!, height: imgHeight!))    
      button.setBackgroundImage(infoImage, forState: .Normal)    
      button.addTarget(self, action: Selector("openInfo"), forControlEvents: UIControlEvents.TouchUpInside)    
      self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
      

      => UIBarButtonItem(image:style:target:action:) 构造函数仅适用于透明 PNG,我还必须设置 clearColor 以外的按钮 tintcolor。

      【讨论】:

        猜你喜欢
        • 2016-06-30
        • 2011-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多