How to find dynamic centre of iOS device screen

13,814

Solution 1

Center of what btw?

You can access the UIViewController's center as self.view.center

or in your case, UIWebView's center as yourwebView.center

And by giving it resizing elements of top left, activityindicator would center it-selves always.

EDIT :

If you want center of the screen that would be gained by frame like

Consider activity indicator of width and height 30, 30.

(([UIScreen mainScreen].bounds.size.width)/2 - 15, ([UIScreen mainScreen].bounds.size.height)/2 - 15, 30, 30);

Then, set autoresizing elements to none.

Go to size Inspector and remove all the arrows from the Auto-sizing feature.

enter image description here

Solution 2

There can be not general solution to your question. This is because only you know what element in your view you want to ignore or not. The view always stays the same, no matter what element you add to it. So, I suggest getting a rectangle for the part of the view you consider as clear/empty/available and setting your loading indicator in that. Just get the view's whole frame (self.view.frame.size.height) and substract any elements from there. For example

MyIndicator *indicator = [[MyIndicator alloc] initWithFrame:CGRectMake(self.toolbar.frame.size.width + self.toolbar.frame.origin.x, self.topBar.frame.size.height + self.topBar.frame.origin.y, self.view.frame.size.width - self.toolbar.frame.size.width - self.toolbar.frame.origin.x, self.view.frame.size.height - self.topBar.frame.size.height - self.topBar.frame.origin.y)];
Share:
13,814
Vinod Vishwanath
Author by

Vinod Vishwanath

Updated on June 05, 2022

Comments

  • Vinod Vishwanath
    Vinod Vishwanath almost 2 years

    Possible Duplicate:
    iOS SDK: Moving the button into the center of screen by code
    UIImageView. Zoom and Center to arbitrary rectangle. Can’t determine correct center on screen

    I'm trying to centralise an UIActivityIndicator in a UIWebView(which in turn is a subView of a UIScrollView, which also has a UIToolbar element to the left - although the left-toolbar isn't always visible)

    Similar questions have been asked before, but the point is in finding the center "dynamically" i.e. on change of orientation as well as presence or absence of the left toolbar.

    What's the best approach? Is there any better way to do this than overriding the method shouldAutorotateToInterfaceOrientation?

  • Vinod Vishwanath
    Vinod Vishwanath over 11 years
    Center of the device screen. NOT the center of the WebView or any other UIComponent. Just the center of the device screen and auto-centering for both orientations. Your last sentence isn't clear to me - could you elaborate please?
  • Robotic Cat
    Robotic Cat over 11 years
    @Vinod Vishwanath:The centre property on a view always gives it's centre point specified within the coordinate system of its superview (in points) NOT the centre of the device screen. The two are only equivalent if the view takes up the entire screen. In addition, all UIControls inherit from UIView and so this property is available on all controls.