当前位置:  开发笔记 > IOS > 正文

按下iOS swift后更改UIBarButtonItem图标

如何解决《按下iOSswift后更改UIBarButtonItem图标》经验,为你挑选了1个好方法。



1> Hitendra Sol..:

创建方法将根据状态更新您的按钮 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
}

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

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

推荐阅读
谢谢巷议
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有