2013-12-23 40 views
1

這讓我瘋狂。我有一個webview和一個UIActivityIndi​​cator。UIActivityIndi​​cator無法在中心加載

由於某些原因,它不會加載到web視圖的中心,而是加載到左側。

嘗試了幾個教程來得到這個工作,但無法弄清楚。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view. 
    NSLog(@"PASSED URL IS %@", passedURL); 
    NSString *fullURL = passedURL; 
    //NSString *fullURL = @"http://107.22.183.71/index.php"; 
    NSURL *url = [NSURL URLWithString:fullURL]; 

    activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 

    //Put the indicator on the center of the webview 
    [activityIndicator setCenter:self.view.center]; 

    //Assign it to the property 
    self.activityIndicator=activityIndicator; 

    //Add the indicator to the webView to make it visible 
    [self.webView addSubview:self.activityIndicator]; 


    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [_webView loadRequest:requestObj]; 
} 

我懷疑它的加載webview或指標的順序。

該指標本身工作正常,我已經實現了所有需要的代表。

任何幫助非常感謝。

回答

3

此時未設置視圖的幾何圖形(viewDidLoad)。放在viewWillAppear:UIActivityIndicator,你應該罰款。

1

嘗試[self.view addSubview:self.activityIndicator];代替[self.webView addSubview:self.activityIndicator];

我的猜測是你的web視圖沒有采取整個屏幕,所以你需要將它添加到self.view

+0

這是兩者的組合。您的回答是將其推向中心的最後一步 – EHarpham

0

也許看待self.view和self.webview不有相同的中心。
嘗試通過

[activityIndicator setCenter:self.webView.center]; 
0

幾件事情需要更換

[activityIndicator setCenter:self.view.center]; 

是否使用自動佈局?如果是這樣,您需要添加一個約束,將您的活動指標的中心固定到網絡視圖的中心。

接下來,您的視圖層次結構中的Web視圖在哪裏?

既然你加入你的活動的指標作爲Web視圖的子視圖,它的中心位置應該在網絡視圖的座標系中表示,不會在視圖控制器的主視圖的座標系統。

試試這個:

CGPoint center = CGPointMake(CGRectGetMidX(_webView.bounds), CGRectGetMid(_webView.bounds)); 
[activityIndicator setCenter: center]; 

我不知道,如果你要和把東西放在你的活動的指標頂部的Web視圖的問題。

而不是增加你的活動指標,因爲您的Web視圖的子視圖,您可能希望將其添加到視圖控制器的觀點,放在Web視圖的頂部,放置在那裏居中。

相關問題