Subramanian Raj
Subramanian Raj

Reputation: 389

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.

Upvotes: 2

Views: 2760

Answers (2)

Krunal
Krunal

Reputation: 79646

You can do it making moving sub view from UIScrollView to super view of scrollview like:

Place/set your button over scroll view (not inside scroll view) as shown here in this snapshot. And also set button constraints (position) with respect to super view of your scrollview.

enter image description here

Here is ref. snapshot of hierarchy of position of each view over each-other.

enter image description here

Upvotes: 4

SwiftArchitect
SwiftArchitect

Reputation: 48514

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.

Upvotes: 3

Related Questions