我有以下代码
- (void) loadView {
self.okButton.autoresizingMask = UIViewAutoresizingFlexibleWidth;
CGFloat w = 20;
self.indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 0, w, w)];
indicator.hidesWhenStopped = YES;
self.indicator.center = self.okButton.center;
[self.okButton addSubview:indicator];
indicator.autoresizingMask = UIViewAutoresizingNone;
}
okButton 被添加到复杂的视图层次结构中。我希望指示器正确放置在 okButton 中。但它在 loadview 和 viewDidLoad 中,框架给出了以下值,
<UIButton: 0x210620a0; frame = (10 121; 300 50); opaque = NO; autoresize = W; layer = <CALayer: 0x21061560>>,
<UIActivityIndicatorView: 0x21062540; frame = (-0.5 15; 20 20); hidden = YES; layer = <CALayer: 0x21062640>>
为什么指标的 x 从 5 变为 -0.5 ?
编辑:我应该删除一行,计算指标的位置如下,
// self.indicator.center = self.okButton.center;
CGFloat size = 20, y = (self.okButton.frame.size.height - size)/2;
self.indicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, y, size, size)];