How to center a UIActivityIndicator?

SOLVED: below to solve my problem.

enter image description here

Hey SO

I'm having trouble centering the UIActivityIndicator ID in the view center. As you can see here, this is a little further down than it should be. I have a UIScrollView that later adds a UIImage routine. Before the UIImage loads, 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 can get the CGPoint Center and set the UIActivityIndicator there? I am not sure why it self.view.centerdoes not work.

Thanks in advance.

EDIT: I had to consider the height of the navigation bar and 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
source share
5

:

  • -, self.view.center . , CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);

  • -, , . .

+6

- . , loadView() , :

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)
+4

spinner.center = self.scrollView.center;

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

+3

I'm not sure what is going on, but maybe your UIScrollView is above the screen? You can try printing the size of your UIView to determine if it is within the borders of the screen. Perhaps the UIActivityIndicator is in the center of the UIView (and not in the center of the screen).

+2
source

Set center to viewDidLayoutSubviews.

    - (void) viewDidLayoutSubviews {

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

}

0
source

All Articles