Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center UIActivityIndicator?

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

like image 995
kkSlider Avatar asked Jul 20 '12 22:07

kkSlider


People also ask

What is the use of activity indicator?

Activity indicator is used to present progress of some activity in the app. It can be used as a drop-in for the ActivityIndicator shipped with React Native.

How do you show loading indicator in react native?

import { ActivityIndicator} from 'react-native'; Here are some of the important properties available with ActivityIndicator. For animating the loading indicator.It takes boolean value true to show the indicator and false to hide it. Color to be shown for the loading indicator.


1 Answers

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.

like image 128
Rob Avatar answered Oct 22 '22 06:10

Rob