Change UIBarButtonItem Icon after pressed iOS swift

家住魔仙堡 提交于 2020-01-02 17:25:22

问题


In the viewDidload method, I've declared a button and set 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)

How do I pressed the button from image 'star.png' then change to 'star_filled.png'? and press the button from 'star_filled.png' to 'star.png'?

How to make two functions like

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
}

回答1:


Create Method which will update you Button based on the state of self.isFavourited

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
}

In the viewDidload method, call first time, i.e.

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



回答2:


I'm sure there's a more elegant way to do it, but this is what worked for me:

In 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]
    }

Then create the two #selector functions:

@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
}


来源:https://stackoverflow.com/questions/34542467/change-uibarbuttonitem-icon-after-pressed-ios-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!