UIActivityIndi​​cator未加载到中心

时间:2013-12-23 16:31:04

标签: ios uiwebview uiactivityindicatorview

这让我发疯了。我有一个webview和一个UIActivityIndi​​cator。

由于某种原因,它不会加载到webview的中心,而是加载到左侧。

尝试了几个教程以使其工作但无法弄明白。

- (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或指标的顺序。

指标本身运作正常,我已经实现了所有需要的代表。

任何帮助非常感谢。

4 个答案:

答案 0 :(得分:3)

此时未设置视图的几何图形(viewDidLoad)。将UIActivityIndicator放在viewWillAppear:中,你就可以了。

答案 1 :(得分:1)

尝试使用[self.view addSubview:self.activityIndicator];代替[self.webView addSubview:self.activityIndicator];

我的猜测是你的webView没有占用整个屏幕,所以你需要将它添加到self.view

答案 2 :(得分:0)

也许观看self.view和self.webview没有相同的中心。
尝试替换

[activityIndicator setCenter:self.view.center];

通过

[activityIndicator setCenter:self.webView.center];

答案 3 :(得分:0)

有几件事:

您使用的是自动布局吗?如果是这样,您将需要添加一个约束,将活动指示器的中心固定到Web视图的中心。

接下来,您的视图层次结构中的Web视图在哪里?

由于您将活动指示符添加为Web视图的子视图,因此它的中心位置应在Web视图的坐标系中表示,而不是在视图控制器的主视图坐标系中。

试试这个:

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

我不知道您是否会在网络视图中遇到问题,将事情放在活动指标之上。

您可能希望将其添加到视图控制器的视图中,放置在Web视图的顶部并将其置于中心位置,而不是将您的活动指示器添加为Web视图的子视图。