Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 增加UIView的高度并更改背景颜色-can';我做不到_Ios_Objective C_Uiview_Uiscrollview - Fatal编程技术网

Ios 增加UIView的高度并更改背景颜色-can';我做不到

Ios 增加UIView的高度并更改背景颜色-can';我做不到,ios,objective-c,uiview,uiscrollview,Ios,Objective C,Uiview,Uiscrollview,我在Interface Builder中绘制了一个UIView(scrollInner),现在我想通过编程增加它的高度,并更改它的背景颜色 UIView位于UIScrollView中,滚动操作正常。基本上,我所做的是尝试增加内部UIView,使其与UIScrollView的contentSize相匹配 以下是我迄今为止在viewdideappear方法中得到的信息: CGRect scrollInnerRect = self.scrollInner.frame; scrollInnerRect.

我在Interface Builder中绘制了一个
UIView
(scrollInner),现在我想通过编程增加它的高度,并更改它的背景颜色

UIView
位于
UIScrollView
中,滚动操作正常。基本上,我所做的是尝试增加内部
UIView
,使其与
UIScrollView
的contentSize相匹配

以下是我迄今为止在
viewdideappear
方法中得到的信息:

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.

检查是否启用了自动布局,如果启用了,请删除间距。并按视图增加子视图的高度。frame=CGrectMake(x,y,yourwidth,yourIncreasedHight)

您可以在interface builder中将连接从UIView添加到类扩展以创建属性。在那之后,你可以像那样对代码进行修改

self.myView.frame = self.scrollInner.contentSize.
您可以这样更改UIView的颜色:

[self.myView setBackgroundColor:[UIColor redColor]];

禁用identity inspector中的“use Autolayout”属性在xib文件中正确检查您的插座,然后编写以下代码:

CGRect scrollInnerRect = self.scrollInner.frame;

scrollInnerRect.size.height = CGrectMake(x,y,320,1000);

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

我们可以创建并保留视图高度的
NSLayoutConstraint
参考

@property (weak, nonatomic) IBOutlet NSLayoutConstraint * scrollInnerHeightConstraint;
我们可以在任何需要的地方使用这个高度约束来更新视图高度

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

谢谢-你说的“删除间距”是什么意思?我也尝试过按您描述的方式增加高度,得到了相同的结果。您启用了自动布局吗?您的uiview似乎使用自动布局连接到了其他视图。检查它是哪一个,并删除该视图的剪辑。您必须删除约束,请尝试[yourview removeConstraints:yourview.constraints];谢谢,在自动布局关闭的情况下,它可以正常工作。但是我需要打开autolayout来管理视图中的其他内容。如何检查UIView连接到哪些其他视图?谢谢-我在更改颜色方面没有问题。我的问题是子视图的高度不会改变!尝试替换此行self.scrollInner.frame=scrollInnerRect;使用该行:self.scrollInner.contentSize=scrollInnerRect;这不起作用,因为UIView对象没有contentSize属性。很抱歉,我以为scrollInner的类型是UIScrollView。您能否确保您的scrollView内容大小也设置为高度=1000。我相信您的uiview的高度为1000,但您的scrollView的内容大小小于1000,您无法看到它。对不起,是的,scrollInner是uiview。我已经将UIScrollView的contentSize属性设置为1000的高度,该高度等于内部UIView的所需高度,并且我完全可以滚动到该距离,因此它确实可以按预期工作。问题是scrollInner的高度没有增加。我想使用Autolayout。我也有同样的问题-UIView的框架已正确更改,但UIView的“扩展”部分是透明的。创建NSLayoutConstraint属性有何帮助?