【问题标题】:UIScrollView contentLayoutGuide and zooming centeredUIScrollView contentLayoutGuide 和缩放居中
【发布时间】:2018-02-11 02:18:25
【问题描述】:

这里要解决的问题是如何在保持居中的同时放大 UIScrollView。如果你不采取某种预防措施,默认是当我们缩小时,缩放后的视图会向上滑动到滚动视图的左上角,如下所示:

那么如何防止这种情况,并在我们缩放时将缩放视图保持在中心?正如您可能知道的那样,有一些传统的方法可以通过弄乱滚动视图的布局来处理这个问题,正如 Josh 和 Eliza 在出色的经典 WWDC video 104 from 2010 中所描述的那样。这可以通过使用委托或通过子类化 UIScrollView 来完成,并提供所需的结果:

现在是 WWDC 2017 视频 201 (https://developer.apple.com/videos/play/wwdc2017/201/?time=1496),Eliza 声称新的 (iOS 11) contentLayoutGuide 解决了缩放问题,同时以一种新的方式保持居中:她说让内容居中在内容布局指南的中心查看。

但她没有展示。当我自己尝试时,我发现它并没有解决问题。我放大 in 很好,但是当缩小 out 时,缩放比例小于 1,内容视图向上移动到左上角,就像它一样一直都有。

有没有人弄清楚视频中的这个声明的真正含义? iOS 11 如何让居中缩放比过去更容易?

编辑我实际上收到了 Apple 的一个示例项目,以回应我的错误报告,他们声称说明了如何解决这个问题,但事实并非如此!所以我得出的结论是,即使是苹果也不知道他们在说什么。

【问题讨论】:

  • @IMcD23 这不是问题;在保持居中的同时进行缩放的方式早已确立。我的问题是视频中声称有一些新方法可以做到这一点,涉及contentLayoutGuide;我仍然没有发现任何迹象支持这种说法。
  • 我认为这属于微不足道的,所以我没有编辑,但这链接到提出索赔的那一刻developer.apple.com/videos/play/wwdc2017/201/?time=1496
  • 有同样的问题。 wwdc 中没有示例或适当的文档,只有两行代码引入了热烈的掌声,然后没有跟进。很想看看是否有人知道如何做到这一点。
  • 这似乎仍然是 iOS 12 的问题,想看看它应该如何工作
  • @matt 你能给我们看看苹果的示例代码吗?我对 Apple 认为它应该如何工作很感兴趣:D。

标签: uiscrollview zooming ios11


【解决方案1】:

这是您/每个人都在寻找的解决方案。在我的情况下,我想在表格视图滚动视图中居中视图。因此,如果表格视图滚动,自定义视图将始终位于滚动视图内容的中心。

// create a view
let v:UIView = UIView(frame:CGRect.zero)    // use zero if using constraints
    ibTableView.addSubview(v)
    ibTableView.bringSubview(toFront:v)
v.translatesAutoresizingMaskIntoConstraints = no
v.backgroundColor = .yellow
v.widthAnchor.constraint(equalToConstant:100).isActive = yes
v.heightAnchor.constraint(equalToConstant:100).isActive = yes

// set scrollview guides
ibTableView.contentLayoutGuide.widthAnchor.constraint(equalTo:ibTableView.frameLayoutGuide.widthAnchor).isActive = yes
ibTableView.contentLayoutGuide.heightAnchor.constraint(equalTo:ibTableView.frameLayoutGuide.heightAnchor).isActive = yes

// anchor view
v.centerXAnchor.constraint(equalTo:ibTableView.contentLayoutGuide.centerXAnchor).isActive = yes
v.centerYAnchor.constraint(equalTo:ibTableView.contentLayoutGuide.centerYAnchor).isActive = yes

【讨论】:

  • 不,这是关于缩放(特别是关于在缩放时保持可缩放视图居中)。你没看懂问题。
  • 您为什么不实际尝试解决方案,然后添加缩放?由于没有人理解您的问题……也许您应该改写它。
  • 很好,照做了。
【解决方案2】:

由于未定义滚动视图的 contentSize,因此视图转到左上角。在 iOS 11 中使用新的 Auto Layout guides 时,仍然需要定义 contentSize。

添加以下约束:

scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
scrollView.contentLayoutGuide.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor)

当我有一个具有固定宽度/高度和以下附加约束的 contentView 时,这对我有用:

//  give the centerView explicit height and width constraints
centerView.widthAnchor.constraint(equalToConstant: 500),
centerView.heightAnchor.constraint(equalToConstant: 500),

//  pin the center of the centerView to the center of the scrollView's contentLayoutGuide
centerView.centerXAnchor.constraint(equalTo: scrollView.contentLayoutGuide.centerXAnchor),
centerView.centerYAnchor.constraint(equalTo: scrollView.contentLayoutGuide.centerYAnchor)

【讨论】:

  • 我试过你的代码(事实上我已经试过了,但我复制并粘贴了它,所以不会出错)但它没有用:我们不会在什么时候保持居中我们缩小,我们也不会连贯地滚动。一定有什么事情你没有告诉我。你能在 GitHub 上发布一个演示吗?谢谢。
猜你喜欢
  • 2017-01-06
  • 2012-02-12
  • 2011-09-30
  • 1970-01-01
  • 2017-09-23
  • 2014-02-16
  • 2017-01-20
  • 1970-01-01
  • 2015-07-10
相关资源
最近更新 更多