在 Navigation Bar 添加喜欢的爱心图标作为 Bar Button Item,点击时触发不同的外观
Adding a favourite love icon as a Bar Button Item at Navigation Bar, trigger to different appearance when clicked
- 如何在导航栏右侧添加心形图标?添加一个 48x48px png 心形图标作为 Bar Button Item 的方法是否正确?
- 我希望在用户单击时更改心形图标的外观。在最喜欢的和不最喜欢的之间切换。请看这个Link
当用户 return 对此 VC 时,我需要将图标标记为最喜欢或不最喜欢。
谁能指点一下。谢谢你。
回答
我认为添加 UIBarButtonitem 是正确的方法,您可以 add/update 带渲染模式的条形按钮图像。
试试这个。
let heartIcon = //UIImage
barButtonItem.image = heartIcon.withRenderingMode(.alwaysOriginal)
已编辑
您可以为最喜欢的和不最喜欢的使用不同的图像。
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)
回答
1st 转到 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]
- 如何在导航栏右侧添加心形图标?添加一个 48x48px png 心形图标作为 Bar Button Item 的方法是否正确?
- 我希望在用户单击时更改心形图标的外观。在最喜欢的和不最喜欢的之间切换。请看这个Link
当用户 return 对此 VC 时,我需要将图标标记为最喜欢或不最喜欢。
谁能指点一下。谢谢你。
回答
我认为添加 UIBarButtonitem 是正确的方法,您可以 add/update 带渲染模式的条形按钮图像。 试试这个。
let heartIcon = //UIImage
barButtonItem.image = heartIcon.withRenderingMode(.alwaysOriginal)
已编辑
您可以为最喜欢的和不最喜欢的使用不同的图像。
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)
回答
1st 转到 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]