[英]iOS Scroll View doesn't scroll with auto layout with views added in programmatically
I have a ViewController
with a scrollView
and a content View
inside.我有一个带有scrollView
View
和内容View
的ViewController
。 Some labels
and stuff inside the Content View
as well. Content View
中还有一些labels
和Content View
。
Now after that, when the view
load i have to pass in data from other places to this view controller
, and then load some other views by code.现在之后,当view
加载时,我必须将数据从其他地方传递到此view controller
,然后通过代码加载其他一些视图。
The scrollview
Content height
and height did get updated, and i did place the loading and adding of the views at ViewWillAppear
. scrollview
Content height
和高度确实得到了更新,并且我确实在ViewWillAppear
放置了视图的加载和添加。 But even if the content size is bigger than the height of the scrollview
, i just can't scroll it.但即使内容大小大于滚动scrollview
的高度,我也无法滚动它。
Scroll View
Height: 667 . Scroll View
高度: 667 。 Scroll View
Content Height : 1632 ContentView
Height : 1632 Scroll View
内容高度:1632 ContentView
高度:1632
Your scrollview did not scrolled because you gave fixed size to content view.您的滚动视图没有滚动,因为您为内容视图设置了固定大小。
Don't worry, just make the outlet of height constraint.别着急,做个高度约束的出口就好了。
Check below image , how to crete outlet of constrain,检查下图,如何确定约束出口,
Note :- You need to create out let of Height Constrain.注意:- 您需要创建出高度约束。
From your image.从你的形象。
And now set in your viewDidLayoutSubviews现在在您的viewDidLayoutSubviews 中设置
-(void)viewDidLayoutSubviews
{
NSInteger screenheight=[ [ UIScreen mainScreen ] bounds ].size.height;
if (screenheight > 504 + 64)
{
_contentviewHeight.constant = screenheight-64;
}
}
And last but not the least , Don't forget to remove Tick from Adjust Scrollview insects ,最后但并非最不重要的一点,不要忘记从Adjust Scrollview 昆虫中删除 Tick ,
In case of Autolayout, whatever view you are adding to scrollview.在自动布局的情况下,您添加到滚动视图的任何视图。 You have to add constraint (basically top constraint) so that Scrollview would calculate contentsize automatically.您必须添加约束(基本上是顶部约束),以便 Scrollview 自动计算 contentsize。
refer below to add constraints programatically.请参阅下面以编程方式添加约束。
iOS create NSLayoutConstraint programmatically for table view cell content iOS 以编程方式为表格视图单元格内容创建 NSLayoutConstraint
Edit :-编辑 :-
Like you said SView is in storyboard.就像你说的 SView 在故事板中。 So create the constraint from storyboard itself on SView.因此,在 SView 上从故事板本身创建约束。
And E,R and H View will be added constraint programatically并且 E、R 和 H 视图将以编程方式添加约束
And the link is just an example链接只是一个例子
You will use [self addConstraint] instead of [self.ContentView addConstraint]您将使用 [self addConstraint] 而不是 [self.ContentView addConstraint]
Moreover refer this one too :-此外,也请参考这个:-
iOS Autolayout Vertically equal space to fill parent view iOS Autolayout 垂直等距填充父视图
OKAY, Finally. OKAY,最后。
After trying and retrying and to no avail.在尝试和重试后,无济于事。 I just gave up on putting auto layout on the ViewController which have the scroll view itself.我只是放弃在具有滚动视图本身的 ViewController 上放置自动布局。
So I untick the auto layout, and then VOILA.所以我取消自动布局,然后VOILA。
Apparently, the view controller which have the scroll view don't need autolayout.显然,具有滚动视图的视图控制器不需要自动布局。 Just the views that i put in via code need auto layout in their storyboard.只是我通过代码放入的视图需要在他们的故事板中自动布局。 After i adjust all the constraints in the view properly and never giving them aspect ratio constraints but just fixed their width and height.在我正确调整视图中的所有约束并且从不给它们纵横比约束而只是固定它们的宽度和高度之后。 Done.完毕。
So I hope anybody who have the same frustration as me see this and heed my advice.所以我希望任何和我有同样挫折感的人都能看到这一点并听取我的建议。 Don't bother putting auto layout on the scroll view or its content View.不要费心在滚动视图或其内容视图上放置自动布局。 Just Untick it altogether.完全取消它。 Do the autolayout for the views you want to put in. You just have to specify the views' width and height properly and it will work.对要放入的视图进行自动布局。您只需正确指定视图的宽度和高度即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.