ios – 在UIButton内放置UIActivityIndi​​cator

我知道我错过了一些愚蠢的东西,但无论如何,这里是我的代码
UIActivityIndicatorView indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.hidesWhenStopped = YES;
indicator.frame = btnLogin.frame;
indicator.center = btnLogin.center;
[self.view addSubview:indicator];
[indicator bringSubviewToFront:indicator];

这是最终结果:

http://img542.imageshack.us/img542/8172/uiactivity.png

先谢谢你!

解决方法

我认为你所缺少的概念是视图的框架(及其中心)与其超视角有关.根据你的屏幕截图,我猜想你的文本框和按钮都在一个作为容器的视图中.所以你的按钮的框架&中心与该容器视图相关,而不是视图控制器的整体视图.你分配相同的帧&中心到活动指示器,但是您将指标添加为主视图的子视图,而不是容器视图.所以你现在有两个视图(按钮和指示符)具有相同的框架,但该框架与两个不同的超级视图相关.

最简单的更改只是将您的指标添加到您正在使用的容器视图.但是我建议将该指标添加为按钮的子视图,然后只做一点数学来调整其位置.

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
CGFloat halfButtonHeight = btnLogin.bounds.size.height / 2;
CGFloat buttonWidth = btnLogin.bounds.size.width;
indicator.center = CGPointMake(buttonWidth - halfButtonHeight,halfButtonHeight);
[btnLogin addSubview:indicator];
[indicator startAnimating];

作为附注:您在视图框架之后的视图中心的设置是多余的.此外,作为子视图添加的最后一个视图将自动为前视图.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...