6

SOLVED: Check below for the solution to my problem.

enter image description here

Hey SO,

I'm having trouble centering the UIActivityIndicator in the center of the view. As you can see here, it's a little farther down vertically than it should be. What I have is a UIScrollView that later adds a UIImage subview. Before the UIImage loads though, I have this UIActivityIndicator to show that the image is loading.

- (void)viewDidLoad
{

    [super viewDidLoad];
    self.scrollView.backgroundColor = [UIColor blackColor];
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc]       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.center = self.view.center;

    [spinner startAnimating];
    [self.view addSubview:spinner];

Any ideas on how I could get the center CGPoint and set the UIActivityIndicator there? I'm not sure why self.view.center doesn't work.

Thanks in advance.

EDIT: I had to account for the height of the navigation bar and the tab bar. The code I had to add was :

float navigationBarHeight = [[self.navigationController navigationBar] frame].size.height;
float tabBarHeight = [[[super tabBarController] tabBar] frame].size.height;
spinner.center = CGPointMake(self.view.frame.size.width / 2.0, (self.view.frame.size.height  - navigationBarHeight - tabBarHeight) / 2.0);

enter image description here

5 Answers 5

7

Two problems:

  1. First, self.view.center is the center of the current view's frame on it's parent's frame. If you want the center of the current view, you want CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);

  2. Second, your current view does not include the navigationbar, so you're centering it on the space of the screen below the nav bar. That will also make it look lower.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Robert Ryan! Going off your two hints I was able to get it working properly. I had to find the height of both the navigation bar and the tab bar in order to get the proper height. I added the code to my original post.
5

SWIFT

I was having the same issue because of a nav bar and a tab bar. To fix, place the following code in your loadView() or wherever you call your activity indicator to begin animating:

let navigationBarHeight = self.navigationController?.navigationBar.frame.height
let tabBarHeight = super.tabBarController!.tabBar.frame.height
YourActivityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, (self.view.frame.size.height - navigationBarHeight! - tabBarHeight) / 2.0)

Comments

4

use spinner.center = self.scrollView.center; instead

try this

spinner.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));

Comments

3

I'm not sure what is exactly going on, but maybe your UIScrollView is taller than the screen? You could try printing the size of your UIView to determine if is in the screen bounds. Perhaps the UIActivityIndicator is on the center of the UIView (not the center of the screen).

2 Comments

Wouldn't the center of the UIView's bounds be the same as the center of the screen?
NSLog gave me that both self.view and self.scrollView's CGRect is {{0, 0}, {320, 460}}. And self.view.center correctly prints (160, 230). Not sure what's wrong here.
0

Set center in viewDidLayoutSubviews.

    - (void) viewDidLayoutSubviews {

self.myActivityIndicator.center = self.view.center;    

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.