UIImageView as a link to a URL

2019-05-26 14:04发布

How to make a UIImageView open a URL in Safari when user clicks it?

1条回答
乱世女痞
2楼-- · 2019-05-26 15:00

I would suggest using a button for this purpose instead. You can create a custom UIButton with whatever image you want. This gives the advantage of providing the built-in target-action mechanism of a button, as well as being able to provided a highlighted image to provide the user feedback. Consider using something like this:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"regular_image.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"highlighted_image.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(loadURL) forControlEvents:UIControlEventTouchUpInside];

Note that it will still look like "just an image" to the user.

查看更多
登录 后发表回答