2012-07-20 79 views
5

已解答:請查看下面的問題解決方案。如何居中UIActivityIndi​​cator?

enter image description here

嗨SO,

我無法在視圖的中心爲中心的UIActivityIndi​​cator。正如你在這裏看到的那樣,它的垂直向下應該比它應該更遠一點。我有一個UIScrollView,後來添加了一個UIImage子視圖。在UIImage加載之前,我有這個UIActivityIndi​​cator來顯示圖像正在加載。

- (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]; 

任何關於如何獲得中心CGPoint和設置UIActivityIndi​​cator的想法?我不知道爲什麼self.view.center不起作用。

在此先感謝。

編輯:我不得不考慮導航欄和標籤欄的高度。我不得不添加的代碼是:

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

回答

6

兩個問題:

  1. 首先,self.view.center是當前視圖的上框架的中心的父的幀。如果你想要當前視圖的中心,你需要CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0);

  2. 其次,你當前的視圖不包括導航欄,所以你將它置於導航欄下面的屏幕空間中。這也會讓它看起來更低。

+0

謝謝@羅伯特瑞恩!消除你的兩個提示,我能夠正常工作。爲了獲得適當的高度,我必須找到導航欄和標籤欄的高度。我將代碼添加到了我原來的帖子中。 – kkSlider 2012-07-20 22:50:15

2

我不知道究竟是什麼事,但也許你的UIScrollView比屏幕高?你可以嘗試打印你的UIView的大小,以確定是否在屏幕邊界。也許UIActivityIndi​​cator位於UIView的中心(不是屏幕的中心)。

+0

豈不UIView的邊界的中心是一樣的屏幕中心? – kkSlider 2012-07-20 22:21:17

+0

NSLog給了我self.view和self.scrollView的CGRect都是{{0,0},{320,460}}。並且self.view.center正確打印(160,230)。不知道這裏有什麼問題。 – kkSlider 2012-07-20 22:26:18

3

use spinner.center = self.scrollView.center;代替

嘗試這種

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

+0

謝謝,但我已經試過這個並得到了相同的結果。 – kkSlider 2012-07-20 22:20:43

4

SWIFT

我有,因爲導航欄和標籤欄的同樣的問題。要修復,將在你的loadView()或是其他地方,你打電話給你活動的指標,開始動畫下面的代碼:在viewDidLayoutSubviews

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) 
0

集中心。

- (void) viewDidLayoutSubviews { 

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

}

相關問題