按下后更改 UIBarButtonItem 图标 iOS swift

Change UIBarButtonItem Icon after pressed iOS swift

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
}

创建将根据 self.isFavourited

的状态更新 Button 的方法
var isFavourited = false;//declare this above the viewDidload()

func updateRighBarButton(isFavourite : Bool){
    let btnFavourite = UIButton(frame: CGRectMake(0,0,30,30))
    btnFavourite.addTarget(self, action: "btnFavouriteDidTap", forControlEvents: .TouchUpInside)


    if isFavourite {
         btnFavourite.setImage(UIImage(named: "star_filled"), forState: .Normal)
    }else{
        btnFavourite.setImage(UIImage(named: "star"), forState: .Normal)
    }
    let rightButton = UIBarButtonItem(customView: btnFavourite)
    self.navigationItem.setRightBarButtonItems([rightButton], animated: true)
}



func btnFavouriteDidTap()
{
    //do your stuff
    self.isFavourited = !self.isFavourited;
    if self.isFavourited {
        self.favourite();
    }else{
        self.unfavourite();
    }
    self.updateRighBarButton(self.isFavourited);
}


func favourite()
{
    //do your favourite stuff/logic
}

func unfavourite(){
    //do your unfavourite logic
}

在viewDidload方法中,第一次调用,即

self.updateRighBarButton(self.isFavourited);//first time self.isFavourited will be false

我敢肯定有更优雅的方法,但这对我有用:

在 viewDidLoad() 中:

    let share_button = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
    let fave_button = UIBarButtonItem(image: UIImage(named: "icon-star"), style: .plain, target: self, action: #selector(favorite))
    let faved_button = UIBarButtonItem(image: UIImage(named: "icon-star-filled"), style: .plain, target: self, action: #selector(unfavorite))
    if YOUROBJECT.faved{
        navigationItem.rightBarButtonItems = [share_button, faved_button]
    }
    else{
        navigationItem.rightBarButtonItems = [share_button, fave_button]
    }

然后创建两个#selector 函数:

@objc func favorite(){
    let share_button = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
    let faved_button = UIBarButtonItem(image: UIImage(named: "icon-star-filled"), style: .plain, target: self, action: #selector(unfavorite))
    navigationItem.rightBarButtonItems = [share_button, faved_button]
    YOUROBJECT.faved = true
}
@objc func unfavorite(){
    let share_button = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))
    let fave_button = UIBarButtonItem(image: UIImage(named: "icon-star"), style: .plain, target: self, action: #selector(favorite))
    navigationItem.rightBarButtonItems = [share_button, fave_button]
    YOUROBJECT.faved = false
}