【发布时间】:2016-04-05 04:36:30
【问题描述】:
在viewDidload 方法中,我声明了一个按钮并设置了 RightBarButton...
let btnFavourite = UIButton(frame: CGRectMake(0,0,30,30))
btnFavourite.addTarget(self, action: "btnFavourite:", forControlEvents: .TouchUpInside)
btnFavourite.setImage(UIImage(named: "star"), forState: .Normal)
btnFavourite.setImage(UIImage(named: "star_filled"), forState: .Highlighted)
let rightButton = UIBarButtonItem(customView: btnFavourite)
self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
如何从图像“star.png”按下按钮然后更改为“star_filled.png”?并按下从“star_filled.png”到“star.png”的按钮?
如何使两个函数像
func btnFavourite()
{
//clicked the favourite button then image change to star_filled.png
}
fun btnUnfavourite()
{
//clicked the button then the bar button image change to star.png
}
【问题讨论】:
-
你已经设置了回调
btnFavourite,你有没有尝试改变那里的图像? -
您拥有的代码应该可以正常工作,实际上突出显示的状态会在您触摸一次并松开手时改变图像,就像悬停动作一样。你想达到什么目标是明确的朋友。
-
@luk2302 在那里的 btnFavourite 函数中写什么来改变 UIBarButtonItem 图标?
-
@satheeshwaran 我想将图标更改为另一个图像,而不仅仅是悬停,所以在这种情况下我不能使用 .Highlighted 状态?
-
Apple doc for Highlighted state - "控件的高亮状态。在跟踪过程中触摸进入和退出以及有触摸事件时,控件进入此状态。您可以检索和设置此值通过 highlight 属性。控件的高亮状态。在跟踪过程中,当触摸进入和退出以及发生触摸事件时,控件进入此状态。您可以通过 highlight 属性检索和设置此值。"
标签: ios swift image button uibarbuttonitem