增加 UIView 的高度并改变背景颜色 - 无法使其工作 [英] Increasing height of UIView and changing background colour - can't make it work

查看:21
本文介绍了增加 UIView 的高度并改变背景颜色 - 无法使其工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Interface Builder 中绘制了一个 UIView (scrollInner),现在我想以编程方式增加它的高度并改变它的背景颜色.

I've drawn a UIView (scrollInner) in Interface Builder and I now want to increase its height programmatically and also change its background colour.

UIView 位于 UIScrollView 内,滚动一切正常.基本上,我正在做的是尝试增加内部 UIView 使其与 UIScrollView 的 contentSize 匹配.

The UIView is inside a UIScrollView, and the scrolling all works fine. Basically, what I'm doing is trying to increase the inner UIView so it matches the contentSize of the UIScrollView.

这是我目前在 viewDidAppear 方法中的内容:

Here's what I've got so far in my viewDidAppear method:

CGRect scrollInnerRect = self.scrollInner.frame;
scrollInnerRect.size.height = 1000;
self.scrollInner.frame = scrollInnerRect;
[self.scrollInner setBackgroundColor:[UIColor redColor]];

UIView 的背景颜色按预期变为红色,但 'UIView' 的高度仍与在 Interface Builder 中设置的大小相同.

The background colour of the UIView changes to red as expected, but the height of the 'UIView' remains the same size as it was set in Interface Builder.

但是,如果我这样做:

NSLog(@"My uiview's frame is: %@", NSStringFromCGRect(scrollInner.frame));

我在日志中得到了这个:

I get this in the log:

My uiview's frame is: {{0, 150}, {320, 1000}}

...这表明 UIView 的高度已经增加.

...which suggests that the UIView's height has been increased.

但这不是它在模拟器中的显示方式.换句话说,在屏幕上 UIView 是红色的,但没有 1000px 的新高度 - 它仍然更短.

But that's not how it appears in the simulator. In other words, on screen the UIView is coloured red, but DOES NOT have the new height of 1000px - it's still way shorter.

有什么我可能会出错的想法吗?

Any ideas where I might be going wrong?

谢谢.

推荐答案

您可能已经解决了这个问题.这可能会帮助其他人.

You might have fixed this issue. This may help others.

我们可以为视图高度创建并保存NSLayoutConstraint的引用.

We can create and hold the reference of NSLayoutConstraint for the view height.

@property (weak, nonatomic) IBOutlet NSLayoutConstraint * scrollInnerHeightConstraint;

我们可以使用这个高度约束来更新我们需要的视图高度

And we can use this height constraint to update the view height wherever we need

scrollInnerHeightConstraint.constant = 1000;//1000 is your height
scrollInnerHeightConstraint.priority = 1000;//1000 is High priority

这篇关于增加 UIView 的高度并改变背景颜色 - 无法使其工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆