Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get center of the displayed area in UIScrollView?

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 hereenter image description here

Thanks!

like image 973
motox Avatar asked Apr 28 '14 05:04

motox


2 Answers

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;
    
like image 71
Anand Suthar Avatar answered Sep 21 '22 01:09

Anand Suthar


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;
like image 40
Himanshu Joshi Avatar answered Sep 22 '22 01:09

Himanshu Joshi