Ios UIActivityIndicator未在中心加载

Ios UIActivityIndicator未在中心加载,ios,uiwebview,uiactivityindicatorview,Ios,Uiwebview,Uiactivityindicatorview,这让我快发疯了。我有一个webview和一个UIActivityIndicator 由于某些原因,它不会加载到webview的中心,而是加载到左侧 尝试了几个教程来实现这一点,但无法解决 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"PASSED URL IS %@", passedURL); NSStri

这让我快发疯了。我有一个webview和一个UIActivityIndicator

由于某些原因,它不会加载到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或指示器的顺序

指示器本身工作正常,我已经实现了所有需要的代理


非常感谢您的帮助

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


我的猜测是您的webView没有占据整个屏幕,因此您需要将其添加到self.view

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


我猜您的webView没有占据整个屏幕,因此您需要将其添加到self中。view

可能视图self.view和self.webView没有相同的中心
试着替换

[activityIndicator setCenter:self.view.center];


可能视图self.view和self.webview没有相同的中心
试着替换

[activityIndicator setCenter:self.view.center];

有几件事:

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

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

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

试试这个:

CGPoint center = CGPointMake(CGRectGetMidX(_webView.bounds), CGRectGetMid(_webView.bounds)); 
[activityIndicator setCenter: center];
我不知道您是否会在web视图中将内容置于活动指示器之上时遇到问题

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

有几件事:

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

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

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

试试这个:

CGPoint center = CGPointMake(CGRectGetMidX(_webView.bounds), CGRectGetMid(_webView.bounds)); 
[activityIndicator setCenter: center];
我不知道您是否会在web视图中将内容置于活动指示器之上时遇到问题


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

此时未设置视图的几何图形(
viewDidLoad
)。将
UIActivityIndicator
放在
视图中将显示:
,您应该没事。

此时未设置视图的几何体(
viewDidLoad
)。将您的
UIActivityIndicator
放入
视图将显示:
,您应该没事。

这是两者的组合。你的答案是向中心移动的最后一步,这是两者的结合。你的回答是向中心移动的最后一步