iOS ScrollView需要y位置或高度的约束
如果我为我的内容视图设置了特定的高度约束(例如1000),ScrollView可以正常工作,但我得到的静态ContentView为1000,这不是我的目标,因为我的ContentView获得了动态内容,因此它应该是内容所需的高度 如果删除ContentView的高度约束,Xcode会显示:iOS ScrollView需要y位置或高度的约束,ios,objective-c,autolayout,storyboard,scrollview,Ios,Objective C,Autolayout,Storyboard,Scrollview,如果我为我的内容视图设置了特定的高度约束(例如1000),ScrollView可以正常工作,但我得到的静态ContentView为1000,这不是我的目标,因为我的ContentView获得了动态内容,因此它应该是内容所需的高度 如果删除ContentView的高度约束,Xcode会显示: -ViewController --View ---ScrollView (Top,Bottom,Leading,Trailing spaces to superview set to 0) ----Cont
-ViewController
--View
---ScrollView (Top,Bottom,Leading,Trailing spaces to superview set to 0)
----ContentView (Top,Bottom,Leading,Trailing spaces to superview set to 0, Width equals with View (ViewController's child))
-----Label1
-----etc...
ScrollView实际上固定在顶部和底部,所以我不知道为什么Xcode想要对ScrollView设置高度约束
当我希望ContentView的高度是内容所需的高度时,正确的方法是什么?它会起作用
Missing Constraints:
ScrollView need constraints for: Y position or height
对ContentView执行同样的操作,这是您自己对内容的计算
-ViewController
--View
---ScrollView (Top,Bottom,Leading,Trailing spaces to superview set to 0)
----ContentView (Top,Leading,Trailing spaces to ScorllView set to 0 | Height to dynamic (1000))
-----Label1
-----etc...
编辑1
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self adjustHeightOfTableview];
}
- (void)adjustHeightOfTableview
{
CGFloat height = tblMainMenu.contentSize.height;
CGFloat maxHeight = tblMainMenu.superview.frame.size.height - tblMainMenu.frame.origin.y;
// if the height of the content is greater than the maxHeight of
// total space on the screen, limit the height to the size of the
// superview.
if (height > maxHeight)
height = maxHeight;
// now set the height constraint accordingly
self.tableViewHeightConstraint.constant = height;
[UIView animateWithDuration:0.1 animations:^{
[self.view setNeedsUpdateConstraints];
}];
}
编辑2
检查网站,这将帮助你。。。
使用scrollview的内容视图设置标签的左上右下边距约束,并将UILable的行数设置为0。它将根据内容的大小扩展内容视图
每当使用带有自动布局的ScrollView时,请始终遵循以下步骤,
巴拉特·莫迪的回答基本上解决了这个问题,但我(Xcode 8.3.2,iOS 10 SDK)还必须将内容视图的高度和宽度与UIScrollView的高度和宽度相匹配 这会将内容大小固定在可见区域,但这会让IB感到高兴。然后(我认为)这只是一个正确计算内容大小并以编程方式进行设置的问题。无论如何我都必须这么做,因为如果你有动态内容,你只能在运行时这样做 如果有导航栏,可能还需要调整视图控制器的“调整滚动视图插入”设置 这就是我所拥有的: 请注意,这可能只是我的设置的一个特点,因为官方建议()也没有提到需要这样做 我试着用一个滚动视图、一个内容视图和另一个固定宽度和高度的虚拟视图来模拟内容,但似乎没有什么能让IB高兴 (对不起,我没有足够的代表将此作为对原始答案的评论)
更新: 因此,实际上我最终要做的是在IB中将我的内容视图设置为固定高度,并在代码中为该高度约束创建一个出口。然后,在视图加载其所有动态内容后,我只需更新高度约束,如下所示:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *OptName_length = @" Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17.0]};
CGRect rect = [OptName_length boundingRectWithSize:CGSizeMake(tableView.bounds.size.width-150.0, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGSize requiredSize = rect.size;
return requiredSize.height +5.0f;
}
一切正常。这里的关键是,当滚动视图中有一个视图时,该视图定义了滚动视图的内容大小。正如人们之前提到的,为了让IB满意,必须在构建时完全定义内容视图的大小。您可以在运行时通过outlet自由地操作这些值,这既简单又安全
在IB中以固定大小设置内容视图的一个优点是,由于您已经固定了内容大小,现在可以像往常一样使用相对偏移来定义内容视图内的视图
确保滚动视图中内容视图中最底部的元素的底部空间约束设置为0,并确保内容视图中的所有元素都设置了高度
这使得内容视图可以根据其中的元素调整大小,并且您的滚动视图/内容视图没有固定的高度,您的滚动视图将根据手机的屏幕大小调整大小。一个小技巧-如果您有UIScrollView和UITextView内置抱怨: “滚动视图需要y位置或高度的约束”。或“UIScrollView可滚动内容大小模糊” 只需取消选中IB中UITextView上的默认“已启用滚动”。就像魔术一样工作:)
Xcode 11
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self adjustHeightOfTableview];
}
- (void)adjustHeightOfTableview
{
CGFloat height = tblMainMenu.contentSize.height;
CGFloat maxHeight = tblMainMenu.superview.frame.size.height - tblMainMenu.frame.origin.y;
// if the height of the content is greater than the maxHeight of
// total space on the screen, limit the height to the size of the
// superview.
if (height > maxHeight)
height = maxHeight;
// now set the height constraint accordingly
self.tableViewHeightConstraint.constant = height;
[UIView animateWithDuration:0.1 animations:^{
[self.view setNeedsUpdateConstraints];
}];
}
我已经关注了几个小时的教程,但似乎没有一个是有效的。Xcode 11似乎有一些更新,改变了滚动视图如何使用自动布局
UIScrollView
,并添加顶部、底部、前导和尾部约束ui视图添加到滚动视图中。我们将其称为内容视图李>
添加顶部、底部、前导和尾随