Search code examples
iosswiftuiscrollviewimageviewzooming

Fix the location of a UIButton when zoom in or zoom out image Swift


Original Screen Shot:

Original Screen Shot

I need to show an image, and above it some buttons that correspond to certain clickable areas of the image, the problem is that the image has zoom, and when i zoom the image, the buttons do not stay in the same place as the picture given below:

Problem Screen Shot

My problem is that the button does not appears in the right place like show in original image.

MainStoryBoard Constraints Screen Shot:

Screen Shot


Solution

  • You need to put your views like in below hierarchy;

    -> UIView
     ->ScrollView
       ->UIImageView
     ->UIButton
     ->UIButton
     ->UIButton
    

    So Z position of UIScrollView should be 0 and UIButtons should be on top of It.

    You can get more Info about how to zoom UIImageview with UIScrollView from https://www.raywenderlich.com/560-uiscrollview-tutorial-getting-started

    In addition: UIScrollView and UIButtons constraints should not be related to each other.