如何获取“Star”(收藏)UIBarButtonItem的图像?

时间:2014-04-02 15:16:08

标签: ios iphone

我是iOS新手。我想在我的应用程序的顶部菜单上放一个“星形”按钮。有没有办法让iOS内置的星形按钮类似于UIBarButtonSystemItemAdd

如果没有,我怎样才能获得类似于Apple在iOS中使用的常见明星的图像?

与此类似:

enter image description here

3 个答案:

答案 0 :(得分:1)

一种简单快捷的方法是使用FontawesomeKit 您可以轻松创建如下按钮:

FAKFontAwesome *icon = [FAKFontAwesome starIconWithSize:20];
[icon addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor]];
UIBarButtonItem *favButton = [[UIBarButtonItem alloc] initWithImage:[icon imageWithSize:CGSizeMake(20, 20)]
                                                         style:UIBarButtonItemStylePlain
                                                        target:self
                                                        action:@selector(doSomething:)];

答案 1 :(得分:0)

我认为这是不可能的。但是,如果您想要一些图标,可以查看this linkanother one

修改

您还可以提取组件的图稿:请参阅this repository

答案 2 :(得分:0)

跟随andreamazz所说的,这里的代码是在navBaritem(swift)中有一个彩色星(喜欢)按钮:

    let favAwesome = FAKFontAwesome.starIconWithSize(30.0) as FAKFontAwesome
    favAwesome.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor())
    let favBtn = UIButton.buttonWithType(UIButtonType.System) as UIButton
    favBtn.frame = CGRectMake(0,0,30,30)
    favBtn.setImage(favAwesome.imageWithSize(CGSizeMake(30, 30)), forState: .Normal)
    favBtn.addTarget(self, action: "favorite", forControlEvents: UIControlEvents.TouchUpInside)
    // favBarBtn is a class instance so we can access in the action method
    favBarBtn = UIBarButtonItem(customView: favBtn)
    navigationItem.rightBarButtonItem = favBarBtn

    // your custom code here to find isFavorite
    if isFavorite {
        favBtn.tintColor = UIColor.redColor()
    } else  {
        favBtn.tintColor = UIColor.blueColor()
    }

然后有“最喜欢的”方法来改变颜色(并设置/取消设置收藏夹):

 func favorite() {
    // your custom favoriting code here
    let favBtn = favBarBtn?.customView as UIButton!
    if isFavorite {
        favBtn.tintColor = UIColor.redColor()
    } else {
        favBtn.tintColor = UIColor.blueColor()
    }
}