【问题标题】:What is the correct way to use UIScrollView?使用 UIScrollView 的正确方法是什么?
【发布时间】:2015-02-05 11:21:11
【问题描述】:

我尝试设置一个简单的视图来显示带有标题的纯文本文章。以下是我已完成的步骤:

  1. 使用其 .xib 文件创建视图控制器,
  2. 创建UIScrollView并将UIView直接放置为内容包装器,
  3. 在顶部、底部、前导和尾部设置约束 [scrollview]-0-[superview]
  4. 在顶部、底部、前导和尾部设置约束 [content wrapper]-0-[scroll view]
  5. 将 Width 和 Height 设置为内容包装器作为占位符。
  6. LabelUITextView 添加为内容包装器的子视图。
  7. 为子视图添加约束。
  8. this tutorial 之后,我以编程方式设置内容包装器的前导 = 向左滚动视图的超级视图,
  9. ... 并且内容包装器的尾随 = 滚动视图的超级视图。

当我运行代码时,它完美地显示了一切; UIScrollView 滚动,边距设置正确等。

但是,Xcode 会抛出异常:

2015-02-05 18:06:58.230 ScrollViewApp[5353:180401] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7ff9fa49a3f0 H:[UIView:0x7ff9fa571560(600)]>",
    "<NSLayoutConstraint:0x7ff9fa49b1a0 H:|-(0)-[UIView:0x7ff9fa571560]   (Names: '|':UIScrollView:0x7ff9fa49a910 )>",
    "<NSLayoutConstraint:0x7ff9fa49ce00 H:|-(0)-[UIScrollView:0x7ff9fa49a910]   (Names: '|':UIView:0x7ff9fa49a840 )>",
    "<NSLayoutConstraint:0x7ff9fa61c050 UIView:0x7ff9fa571560.right == UIView:0x7ff9fa49a840.trailing>",
    "<NSLayoutConstraint:0x7ff9fa580970 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7ff9fa49a840(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7ff9fa49a3f0 H:[UIView:0x7ff9fa571560(600)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

进一步谷歌搜索后,我发现我可以通过将内容包装器的宽度(参见上面的步骤 5)优先级设置为低来消除警告;但是它破坏了界面生成器中的布局。看看:

与设置为高优先级时相比:

我知道最终它没有任何区别,因为它都按预期工作..但我有点好奇为什么会发生这些事情..

我试图了解 UIScrollView 是如何工作的,但也许我误解了一些东西;那么,设置 UIScrollView 以按预期工作的正确方法是什么?

【问题讨论】:

  • 尝试从内容包装器向滚动视图添加中心水平约束。
  • @JadFeitrouni 你是对的;它确实解决了上面的问题!但是我想知道为什么..从错误日志中,无法正确确定“我需要添加中心水平约束”..如何更好地解决这些问题?
  • 说实话,我真的不知道为什么会这样,我通过试错发现了这个。如果您希望您的视图兼容不同的屏幕尺寸,请移除内容包装器的宽度限制。
  • 感谢您的提示。请提交答案,我会尽快接受。

标签: ios objective-c xcode xib


【解决方案1】:

尝试从内容包装器向滚动视图添加中心水平约束。 老实说,我真的不知道为什么会这样,我通过试错发现了这个。如果您希望您的视图兼容不同的屏幕尺寸,请移除内容包装器的宽度约束。 希望对您有所帮助。

【讨论】:

    【解决方案2】:

    你可以像这样使用滚动视图。在设计中添加滚动视图。在设计中给出它的左、上、右、下约束。在滚动视图中添加您的子视图。为它们添加适当的约束。无需在代码中添加约束。在 viewDidLayoutSubviews 中设置滚动视图的内容大小。 - (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];
    CGRect boundsOfSelf = self.view.bounds;
    scrollView.frame = boundsOfSelf;
    [scrollView setContentSize: boundsOfSelf.size];
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2021-09-23
      • 2017-04-07
      • 2013-01-09
      • 2013-03-19
      • 2019-05-11
      • 2018-02-17
      相关资源
      最近更新 更多