当用户返回此VC时,我需要标记喜欢或不喜欢的图标。
请有人指点一下。谢谢。
答案
答案 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];
}
}
[![!\[Pic][2]][2]