【发布时间】:2011-04-26 04:33:19
【问题描述】:
当我将子视图添加到 UIView 时,或者当我调整现有子视图的大小时,我希望 [view sizeToFit] 和 [view sizeThatFits] 能够反映这种变化。但是,我的经验是sizeToFit 什么都不做,sizeThatFits 在更改前后返回相同的值。
我的测试项目有一个包含单个按钮的视图。单击该按钮会向视图添加另一个按钮,然后在包含视图上调用sizeToFit。视图的边界在添加子视图之前和之后被转储到控制台。
- (void) logSizes {
NSLog(@"theView.bounds: %@", NSStringFromCGRect(theView.bounds));
NSLog(@"theView.sizeThatFits: %@", NSStringFromCGSize([theView sizeThatFits:CGSizeZero]));
}
- (void) buttonTouched {
[self logSizes];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10.0f, 100.0f, 400.0f, 600.0f);
[theView addSubview:btn];
[theView sizeToFit];
[self performSelector:@selector(logSizes) withObject:nil afterDelay:1.0];
}
输出是:
2010-10-15 15:40:42.359 SizeToFit[14953:207] theView.bounds: {{0, 0}, {322, 240}}
2010-10-15 15:40:42.387 SizeToFit[14953:207] theView.sizeThatFits: {322, 240}
2010-10-15 15:40:43.389 SizeToFit[14953:207] theView.bounds: {{0, 0}, {322, 240}}
2010-10-15 15:40:43.391 SizeToFit[14953:207] theView.sizeThatFits: {322, 240}
我一定是错过了什么。
谢谢。
【问题讨论】:
标签: iphone ios uiview resize uibutton