UIImageView as a link to a URL

2019-05-26 14:05发布

问题:

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

回答1:

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.