Subramanian Raj Subramanian Raj - 4 months ago 18
iOS Question

UIScrollView keep buttons in fixed position

I am using UIScrollView for pinch zooming the image the code I am using is like this

CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame]; //Full Screen
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:fullScreenRect];
scroll.backgroundColor = [UIColor clearColor];
scroll.delegate = self;

scroll.contentSize = _imageView.frame.size;

[scroll addSubview:_imageView];
scroll.minimumZoomScale = 1.0;
scroll.maximumZoomScale = 5.0;
[scroll setZoomScale:scroll.minimumZoomScale];
self.view = scroll;


[scroll addSubview:_screenShotButton];
[scroll addSubview:_resolutionButton];

-(void)aligementTheControlBasedOnOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
bool ipad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
if (ipad) {

if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
_imageView.frame = CGRectMake(142, 114, 740, 555);

if ([self.resoultion isEqualToString:@"Default"]) {
_imageView.frame = CGRectMake(142, 114, 740, 555);
}
else if([self.resoultion isEqualToString:@"4:3"])
_imageView.frame = CGRectMake(142, 114, 740, 555);
else if([self.resoultion isEqualToString:@"16:9"])
_imageView.frame = CGRectMake(142, 114, 740, 416);

self.screenShotButton.frame = CGRectMake(794, 667, 55, 55);

self.resolutionButton.frame = CGRectMake(857, 667, 55, 55);
}
else
{
_imageView.frame = CGRectMake(64, 238, 640, 480);

if ([self.resoultion isEqualToString:@"Default"]) {
_imageView.frame = CGRectMake(64, 238, 640, 480);
}
else if([self.resoultion isEqualToString:@"4:3"])
_imageView.frame = CGRectMake(64, 238, 640, 480);
else if([self.resoultion isEqualToString:@"16:9"])
_imageView.frame = CGRectMake(64, 238, 640, 360);

self.screenShotButton.frame = CGRectMake(578, 749, 55, 55);

self.resolutionButton.frame = CGRectMake(649, 749, 55, 55);
}
}
}


Based on the resolution user has chosen I am positioning the buttons.

Everything is working fine for me. The problem I am facing is _screenShotButton and _resolutionButton are also moving when I zoom the image. Instead I want to keep the buttons static in the bottom right side of the screen.

Can anyone help me.

Answer

Do not add your buttons to the scroll view, but to its parent instead.

This part is wrong:

[scroll addSubview:_screenShotButton];
[scroll addSubview:_resolutionButton];

Assuming scroll has a superview (which in your original question it does not), do this instead:

[scroll.superview addSubview:_screenShotButton];
[scroll.superview addSubview:_resolutionButton];

Alternatively, you can pick the order of scroll vs. screenShotButton by replacing -addSubView: by one of the -insertSubview:: methods.

Comments