简体   繁体   English

带有UIScrollView的UIViewController

[英]UIViewController with UIScrollView

I'm trying to change a UIViewControllers view to a scroll view to make use : 我正在尝试将UIViewControllers视图更改为滚动视图以使用:

def loadView
  super
  scrollView = UIScrollView.alloc.initWithFrame(UIScreen.mainScreen.bounds)
  self.view = scrollView
  # Tried experimenting with this
  # self.view.contentSize = CGSizeMake(UIScreen.mainScreen.bounds.size.width,, UIScreen.mainScreen.bounds.size.height)
  self.view.backgroundColor = UIColor.whiteColor
end

I then add subViews to self.view inside of viewDidLoad , use subview.setTranslatesAutoresizingMaskIntoConstraints(false) on each and then set constraints using NSLayoutConstraint.constraintsWithVisualFormat . 我然后添加子视图self.view的内部viewDidLoad ,使用subview.setTranslatesAutoresizingMaskIntoConstraints(false)上的每个,然后使用设置限制NSLayoutConstraint.constraintsWithVisualFormat

The constraints work fine normally, but as soon as I change the view to a scroll view, everything gets messed up: 约束正常工作正常,但只要我将视图更改为滚动视图,一切都搞砸了:

在此输入图像描述

The "title" field should be flush with the navigation bar, and both fields should fill the screen width. “标题”字段应与导航栏齐平,并且两个字段都应填充屏幕宽度。

The red box is simply demonstrating the width of the subviews. 红色框只是展示了子视图的宽度。

Sometimes it's hard to get UIViews working with autoLayout inside of a UIScrollView. 有时很难让UIViews在UIScrollView中使用autoLayout。 What i do in those cases is set a containerView that fills the UIScrollView four edges with constraints and positioning in autoLayout. 在这些情况下我做的是设置一个containerView,它使用约束和autoLayout中的定位填充UIScrollView四条边。 Then in this container i add what i want, one thing that i always have to do is to keep track of the height of this container and make it grow accordingly to the subviews added to that. 然后在这个容器中添加我想要的东西,我总是要做的一件事就是跟踪这个容器的高度,并使其相应增加到那个子视图。 This way is a bit harder to implement the view hierarchy, but the constraints become more organized. 这种方式实现视图层次结构有点困难,但约束变得更有条理。 Here is a note from Apple of how to do this in a quick way: https://developer.apple.com/library/ios/technotes/tn2154/_index.html 以下是Apple关于如何快速完成此操作的说明: https//developer.apple.com/library/ios/technotes/tn2154/_index.html

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

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