【问题标题】:UIActivityIndicator not loading in centerUIActivityIndi​​cator 未在中心加载
【发布时间】:2013-12-23 16:31:04
【问题描述】:

这快把我逼疯了。我有一个 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 或指示器的顺序。

指标本身工作正常,我已经实现了所有需要的委托。

非常感谢任何帮助。

【问题讨论】:

    标签: ios uiwebview uiactivityindicatorview


    【解决方案1】:

    此时尚未设置视图的几何形状 (viewDidLoad)。将您的UIActivityIndicator 放入viewWillAppear:,您应该会没事的。

    【讨论】:

      【解决方案2】:

      尝试[self.view addSubview:self.activityIndicator]; 而不是[self.webView addSubview:self.activityIndicator];

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

      【讨论】:

      • 这是两者的结合。您的答案是将其移向中心的最后一步
      【解决方案3】:

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

      [activityIndicator setCenter:self.view.center];
      

      通过

      [activityIndicator setCenter:self.webView.center];
      

      【讨论】:

        【解决方案4】:

        几件事:

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

        接下来,您的 Web 视图在视图层次结构中的什么位置?

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

        试试这个:

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

        我不知道您是否会遇到将网络视图放在活动指示器顶部的问题。

        与其将活动指示器添加为 Web 视图的子视图,不如将其添加到视图控制器的视图中,放置在 Web 视图的顶部并使其居中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多