我在Interface Builder中绘制了一个UIView
(scrollInner),现在我想以编程方式增加其高度,并改变其背景颜色。
UIView
位于UIScrollView
内,滚动一切正常。基本上,我正在做的是尝试增加内部UIView
,使其与UIScrollView
的contentSize匹配。
以下是我在viewDidAppear
方法中获得的内容:
CGRect scrollInnerRect = self.scrollInner.frame;
scrollInnerRect.size.height = 1000;
self.scrollInner.frame = scrollInnerRect;
[self.scrollInner setBackgroundColor:[UIColor redColor]];
UIView
的背景颜色按预期变为红色,但“UIView”的高度仍与Interface Builder中设置的大小相同。
但是,如果我这样做:
NSLog(@"My uiview's frame is: %@", NSStringFromCGRect(scrollInner.frame));
我在日志中得到了这个:
My uiview's frame is: {{0, 150}, {320, 1000}}
......这表明UIView
的身高已经增加。
但这并不是它在模拟器中出现的方式。换句话说,在屏幕上UIView
显示为红色,但是没有新的高度1000px - 它仍然更短。
我可能会出错的任何想法?
THX。
答案 0 :(得分:0)
检查是否启用了Autolayout,如果这样,删除间距。并通过view.frame = CGrectMake(x,y,yourwidth,yourincreasedheight)接收子视图的高度
答案 1 :(得分:0)
您可以在界面构建器中将连接从UIView添加到类扩展以创建属性。之后,您可以在代码中进行更改。
self.myView.frame = self.scrollInner.contentSize.
你可以改变你的UIView颜色:
[self.myView setBackgroundColor:[UIColor redColor]];
答案 2 :(得分:0)
禁用身份检查器中的'use Autolayout'属性,在写完此代码后,在xib文件中正确检查您的插座:
CGRect scrollInnerRect = self.scrollInner.frame;
scrollInnerRect.size.height = CGrectMake(x,y,320,1000);
答案 3 :(得分:0)
您可能已修复此问题。这可能对其他人有所帮助。
我们可以为视图高度创建并保留NSLayoutConstraint
的引用。
@property (weak, nonatomic) IBOutlet NSLayoutConstraint * scrollInnerHeightConstraint;
我们可以使用此高度约束来更新我们需要的视图高度
scrollInnerHeightConstraint.constant = 1000;//1000 is your height
scrollInnerHeightConstraint.priority = 1000;//1000 is High priority