简体   繁体   English

带有自动布局的ScrollView

[英]ScrollView With AutoLayout

I have a weird output with autoLayout used for following scenario. 我有一个奇怪的输出与autoLayout用于以下情况。

I have 4 sub pages to scroll. 我有4个子页面可以滚动。 (subPage or scrollPage has been designed separately with autoLayout). (subPage或scrollPage已与autoLayout分开设计)。

MainView has a scrollView component which loads the sub pages. MainView有一个scrollView组件,可加载子页面。

Everything is fine except the starting. 除了开始,一切都很好。 After first load, the sub page components are not arranged properly. 第一次加载后,子页面组件未正确排列。 As soon as it receives a first tap/touch, automatically re-scrolls/re-arranges to proper places. 一旦收到第一次点击/触摸,它就会自动重新滚动/重新安排到正确的位置。 which looks like a bug. 看起来像个虫子。

The loading creates problem. 加载会产生问题。 I have attached 2 images for reference for the above scenarios. 我已附上2张图片以供上述情况参考。 FirstOne at the first loading 首次加载时为FirstOne 在此处输入图片说明

Second one just after tapping on scroll area 点击滚动区域后的第二个 在此处输入图片说明

Second one is the proper one. 第二个是适当的。 I need to show this instead of the first. 我需要展示这个,而不是第一个。 Need help to fix this. 需要帮助解决此问题。

Thanks, Satyaranjan 谢谢,Satyaranjan

One more thing to note, [myScroll scrollRectToVisible:CGRectMake(320*pageNumber, 0, 320 , 240) animated:NO]; 还有一点要注意,[myScroll scrollRectToVisible:CGRectMake(320 * pageNumber,0,320,240)animation:NO]; this is not working As I am using dynamic width for the subPage. 这不起作用,因为我为子页面使用了动态宽度。 Because it will vary for iPhon5 and iPhone6 因为iPhon5和iPhone6会有所不同

First, you generally don't want to set the scrollView's contentSize when using autolayout -- if your sub views are laid out correctly, it will do that automatically. 首先,您通常不希望在使用自动布局时设置scrollView的contentSize -如果正确放置了子视图,它将自动执行此操作。

Try this: 尝试这个:

  • constrain all four of scrollView's edges to its parent 将scrollView的所有四个边约束到其父级
  • make sure translatesAutoresizingMaskIntoConstraints is NO for all views 确保所有视图的translatesAutoresizingMaskIntoConstraints为否
  • Create a view called "contentView" and parent it to the scrollView. 创建一个名为“ contentView”的视图,并将其作为scrollView的父级。
  • pin all four of contentViews's edges to scrollView 将contentViews的所有四个边缘固定到scrollView
  • parent your subviews to contentView and arrange them with autolayout 将您的子视图父到contentView并使用自动布局进行排列

If for some reason you need to change the size of a subview programmaticly, you need to override its intrinsicContentSize method to return the correct size after the change. 如果出于某种原因需要以编程方式更改子视图的大小,则需要重写其内在内容大小方法以在更改后返回正确的大小。 After the change, you might need to call the view's sizeToFit method from its parent view (not sure about that -- it may happen automatically). 更改后,您可能需要从其父视图中调用该视图的sizeToFit方法(对此不确定(它可能会自动发生))。

In general, when using autolayout, you should almost never explicitly set the size of anything. 通常,使用自动版式时,几乎永远不应显式设置任何内容的大小。 If it can't be avoided, you should do it by creating height/width constraints and modifying them at runtime in the updateConstraints method. 如果无法避免,则应通过创建高度/宽度约束并在运行时在updateConstraints方法中对其进行修改来实现。

EDIT: 编辑:

I made an example project which demonstrates how to set up a scrollView and a couple other things. 我制作了一个示例项目,演示了如何设置scrollView和其他一些事情。

Take a look! 看一看!

https://github.com/annabd351/AutolayoutTemplate https://github.com/annabd351/AutolayoutTemplate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM