Question

Set up a UIScrollView of height 1400 with numerous UIButtons. On clicking each of these UIButtons, i reveal a small UIView(hidden when view is loaded) containing some values. I want to display these UIViews at the center of the displayed area of the UIScrollView.

When i click 'TYPE'(behind the UIView in the image), i reveal the view as shown below:

enter image description here

But no matter where 'TYPE' button is and when clicked i want to reveal UIView as shown below(always at the center of the displayed area):

enter image description here enter image description here

Thanks!

Was it helpful?

Solution

Try this

yourCustomAlert.center = self.view.center;

Inside your scrollView

  1. first get visible Rect of scrollview

    CGRect visibleRect = CGRectMake(myScrollView.contentOffset.x, myScrollView.contentOffset.y, myScrollView.bounds.size.width, myScrollView.bounds.size.height)
    
  2. then get it's center

    CGPoint centerPoint = CGPointMake(visibleRect.size.width/2, visibleRect.size.height/2);
    
  3. then set your alertView's center

    yourCustomAlert.center = centerPoint;
    

OTHER TIPS

Your problem is not clear as much. Though, if you want that your hidden view will appear on the center of the screen despite of the scrollView bounds, you can use

yourHiddenView.center = self.view.center; //If Parent class is a ViewController
//yourHiddenView.hidden = NO;

OR

yourHiddenView.center = self.center; //If Parent class is a UIView
//yourHiddenView.hidden = NO;

And, if you want the hidden view to be at the center of ScrollView find the visible rect of the scrollView and find its center

CGRect visibleRect;
visibleRect.origin = scrollView.contentOffset;
visibleRect.size = scrollView.bounds.size;

CGPoint scrollViewCenter = CGPointMake(visibleRect.size.width/2, visibleRect.size.height/2);

yourHiddenView.center = scrollViewCenter;
//yourHiddenView.hidden = NO;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top