在导航栏中添加喜爱的爱情图标作为条形按钮项目,单击时会触发不同的外观

时间:2018-03-13 08:51:34

标签: ios objective-c uinavigationbar uibarbuttonitem uibarbuttonitemstyle

  1. 如何在导航栏的右侧添加心形图标?它是通过添加48x48px png心形图标作为条形按钮项目的正确方法吗?
  2. 我想在用户点击它时更改心形图标的外观。在喜欢和不喜欢之间切换。请参阅此Link
  3. 当用户返回此VC时,我需要标记喜欢或不喜欢的图标。

    请有人指点一下。谢谢。

  4. 答案

    Pic

2 个答案:

答案 0 :(得分:0)

我认为添加UIBarButtonitem是正确的方法,您可以使用渲染模式添加/更新条形按钮图像。 试试这个。

let heartIcon = //UIImage
barButtonItem.image = heartIcon.withRenderingMode(.alwaysOriginal)

<强> EDITED

您可以使用不同的图像作为喜欢和不喜欢。

let heartIcon:UIImage

if (isFavourite){// favourite condition
    //set favourite image icon
    heartIcon = UIImage(imageLiteralResourceName: "favourite")
}else{
    //set unfavourite image icon
    heartIcon = UIImage(imageLiteralResourceName: "unfavourite")
}

//and update you bar button image.
barButtonItem.image = heartIcon.withRenderingMode(.alwaysOriginal)

答案 1 :(得分:0)

答案

1转到fa2png.io以创建20px或30px图标。保存并导入assets.xcassets

然后是代码

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];

BOOL favorite = YES;
if (favorite)
{
     [heart setImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
}else{
     [heart setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];
}

[heart addTarget:self
          action:@selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:heart];

self.navigationItem.rightBarButtonItem = jobsButton;

}

-(void)buttonTapped:(UIButton*)heart
{
if( [[heart imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"notFavorite.png"]])

{
    NSLog(@"favorite.png");

    [heart setImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];

    [UIView animateKeyframesWithDuration:0.5
                                   delay:0.0
                                 options:0.0
                              animations:^{

                                  // Heart expands
                                  [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.10 animations:^{
                                      heart.transform = CGAffineTransformMakeScale(1.3, 1.3);
                                  }];

                                  // Heart contracts.
                                  [UIView addKeyframeWithRelativeStartTime:0.15 relativeDuration:0.25 animations:^{
                                      heart.transform = CGAffineTransformMakeScale(1.0, 1.0);
                                  }];

                              } completion:nil];
}
else
{
    NSLog(@"notFavorite.png");

    [heart setImage:[UIImage imageNamed:@"notFavorite.png"] forState:UIControlStateNormal];

    [UIView animateKeyframesWithDuration:0.5
                                   delay:0.0
                                 options:0.0
                              animations:^{

                                  // Heart expands
                                  [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.10 animations:^{
                                      heart.transform = CGAffineTransformMakeScale(1.3, 1.3);
                                  }];

                                  // Heart contracts.
                                  [UIView addKeyframeWithRelativeStartTime:0.15 relativeDuration:0.25 animations:^{
                                      heart.transform = CGAffineTransformMakeScale(1.0, 1.0);
                                  }];

                              } completion:nil];
    }
}

RESULT

[![!\[Pic][2]][2]