我认为对您的问题的简短回答是,不,不会根据子视图自动调整视图大小。关于您的后一个问题(根据另一个控件调整控件的框架),您应该查看来自WWDC 2012的各种“自动布局”视频。
当我最初回答这个问题时,我认为我可能刚刚提供了解决方案,在重新阅读您的问题后,我认为您可能已经实施了。我很抱歉。无论如何,我包括我的旧答案供您参考。
老答案:
在你的第一个问题上,不,我认为你必须遍历你的子视图来完成你想要的。我不认为你可以用自动调整掩码做任何事情(那些被设计成相反的方式,根据他们的超级视图边界的变化来调整子视图的框架)。虽然 iOS 6 承诺了一些增强功能,但我认为它无法解决您的特定挑战。
不过,你显然可以很容易地以编程方式做一些事情。您可以执行以下操作:
- (void)resizeView:(UIView *)view
{
CGSize maxSize = CGSizeMake(0.0, 0.0);
CGPoint lowerRight;
// maybe you don't want to do anything if there are no subviews
if ([view.subviews count] == 0)
return;
// find the most lowerright corner that will encompass all of the subviews
for (UIView *subview in view.subviews)
{
// you might want to turn off autosizing on the subviews because they'll change their frames when you resize this at the end,
// which is probably incompatible with the superview resizing that we're trying to do.
subview.autoresizingMask = 0;
// if you have containers within containers, you might want to do this recursively.
// if not, just comment out the following line
[self resizeView:subview];
// now let's see where the lower right corner of this subview is
lowerRight.x = subview.frame.origin.x + subview.frame.size.width;
lowerRight.y = subview.frame.origin.y + subview.frame.size.height;
// and adjust the maxsize accordingly, if we need to
if (lowerRight.x > maxSize.width)
maxSize.width = lowerRight.x;
if (lowerRight.y > maxSize.height)
maxSize.height = lowerRight.y;
}
// maybe you want to add a little margin?!?
maxSize.width += 10.0;
maxSize.height += 10.0;
// adjust the bounds of this view accordingly
CGRect bounds = view.bounds;
bounds.size = maxSize;
view.bounds = bounds;
}
只需在您可能想要根据其子视图调整大小的任何“容器”视图上调用它(最好不要将其与适当的视图控制器包含混淆,这是一种不同的野兽)。请注意,我只是调整大小(假设您也不想移动子视图或视图的origin
,如果您愿意,您可以轻松地做到这一点)。我也让这个递归进行,但也许你不想这样做。你的来电。
在第二个问题上,将标签 B 移动到标签 A 下方非常容易:
CGRect frame = b.frame;
frame.origin.y = a.frame.origin.y + a.frame.size.height;
b.frame = frame;