0
votes

I am adding buttons as a subview to UIImageview in scrollview. When I am zooming UIImageView obviously those subview buttons get zoomed with the aspect ratio. I don't want those buttons get zoomed. How can I achieve this. I have already tried to add the button on different view rather than adding it to the UIImageView but it will not maintain the proper x and y coordinates when we zoom image. Please provide me solution thanks in advance

4
Place your buttons on a view and add constraints.Bharat
I am not using autolayout for this applicationNilesh Mahajan
then go for @Sanjay answer..it might help youBharat

4 Answers

1
votes

U can set center of uibutton as a center of uiimageview like below (don't add button in imageview):

[btnControl setCenter:[imgView center]];
1
votes

The solution to this will involve a little mathematical intervention.

Use transform to zoom your image view.

[<yourview> setTransform:CGAffineTransformMakeScale(<xsacle>, <y scale>)];

Now this transform will also apply to all subviews you have to scale down the transform of your button appropriately.

[<Your Button> setTransform:CGAffineTransformMakeScale(1/<xsacle>, 1/<y scale>)];

Now you need too adjust the x and y of button.

0
votes

First need to store your button frame;

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    initialButtonFrame = self.button.frame;
}

and use below code in UIScrollView delegate method.

- (void)scrollViewDidZoom:(UIScrollView *)sv
{

        self.button.frame = CGRectMake((initialButtonFrame.origin.x * self.scrollView.zoomScale),
                               (initialButtonFrame.origin.y * self.scrollView.zoomScale),
                               initialButtonFrame.size.width,
                               initialButtonFrame.size.height);
}
0
votes

You should add those button as subview of some other view. Now add that other view and image view directly to scroll view. Now, you should implement UIScrollViewDelegate method.

-(UIView *)viewForZoomingInScrollView
{
   return imageView;
}

This will zoom the imageview only and not the container view which has buttons as subviews.