【发布时间】:2010-04-19 20:40:20
【问题描述】:
当父视图的边界未知时,相对于其父视图的大小定位视图的最佳方法是什么?
如果可能的话,我会尽量避免硬编码坐标。也许这是愚蠢的,如果是这样,这是一个完全可以接受的答案。
我在使用自定义 UI 时遇到过很多次。最近的例子是我试图用自定义视图替换 UINavigationItem 纯文本标题。我希望该视图填充超级视图,但此外,我希望在右侧有一个UIActivityIndicatorView,插入大约 2 个像素并垂直居中。代码如下:
- (void) viewDidLoad
{
[super viewDidLoad];
customTitleView = [[UIView alloc] initWithFrame:CGRectZero];
customTitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.numberOfLines = 2;
titleLabel.minimumFontSize = 11.0;
titleLabel.font = [UIFont systemFontOfSize:17.0];
titleLabel.adjustsFontSizeToFitWidth = YES;
[customTitleView addSubview:titleLabel];
spinnerView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
spinnerView.center = CGPointMake(customTitleView.bounds.size.width - (spinnerView.bounds.size.width / 2) - 2,
customTitleView.bounds.size.height / 2);
spinnerView.hidesWhenStopped = YES;
[customTitleView addSubview:spinnerView];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
}
这是我的问题:在这段代码运行时,customTitleView.bounds 仍然为零。自动调整大小的蒙版还没有机会做它的事情,但我非常想要这些值,以便我可以计算其他子视图的相对位置(这里是活动指示器)。
这可能不丑吗?
【问题讨论】:
-
titleLabel和spinnerView都是泄露对象 -
+1 问题,因为我有完全相同的问题 - 期待阅读答案
-
titleLabel和spinnerView没有泄露。它们在其他地方发布。
标签: iphone cocoa-touch