在UIView UIButtons
问题描述:
我有一个UIScrollview与IBOutlet映射到ganttScroller。这个UIScrollView有一个UIView。我做了UIView的IB,其宽度为100在UIView UIButtons
然后我开始按钮添加到的UIView(通过一个IBOutlet scrollContent映射)
float test = [scrollContent frame].size.width;
for (int i=0; i<15; i++) {
UIButton *showButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
showButton.frame = CGRectMake(55.0 * i,
50.0,
50.0,
20.0);
[showButton setTitle:NSLocalizedString(@"test", @"") forStates:UIControlStateNormal];
[scrollContent addSubview:showButton];
}
test = [scrollContent frame].size.width;
[scrollContent sizeToFit];
test =[scrollContent frame].size.width;
在开始检查我scrollContent的大小和它确实是100(在调试器中检查'测试'),添加按钮后它又是100,并且在sizeToFit之后它仍然是100(因为所有这些按钮都被添加了,所以我预计会更大)...显示按钮正确! 我需要正确的大小为我的gantScroller(UIScrollView)
什么是错?
答
我不认为-[UIView sizeToFit]
实际上是考虑到它的子视图。您需要简单地计算正确的宽度并将其分配给视图的框架。然后,您还需要设置滚动视图的contentSize
属性,以便它正确滚动。
也许你看到尽管容器太小的所有按钮的原因是UIViews做而不是在默认情况下剪辑它们的子视图。
好的,thx我认为contentSize会计算出对我来说,我需要什么方法来计算正确的宽度? – Glenn 2010-09-24 05:59:36
当你在特定情况下它是'CGRect frame = scrollContent.frame; frame.size.width = 14 * 55 + 50; scrollContent.frame = frame; scrollView.contentSize = frame.size;' – 2010-09-24 12:53:54