我想在scrollview(snapchat样式)中添加3个视图控制器。以下代码似乎工作正常,但当我退出应用程序(转到主屏幕),然后回来时,视图会因某些奇怪的原因而调整大小。我尝试添加alignAllTop和alignAllBottom但没有成功。任何想法为什么会发生这种情况?
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
self.mainScrollView.delaysContentTouches = false
let v1 = storyboard.instantiateViewController(withIdentifier: "profileViewController") as! profileViewController
self.addChildViewController(v1)
self.mainScrollView.addSubview(v1.view)
v1.didMove(toParentViewController: self)
//--------
let v2 : recordViewController = recordViewController(nibName: "recordViewController", bundle: nil)
self.addChildViewController(v2)
self.mainScrollView.addSubview(v2.view)
v2.didMove(toParentViewController: self)
//--------
let v3 = storyboard.instantiateViewController(withIdentifier: "discoverViewController") as! discoverViewController
self.addChildViewController(v3)
self.mainScrollView.addSubview(v3.view)
v3.didMove(toParentViewController: self)
//--------
var v1frame : CGRect = self.mainScrollView.bounds
v1frame.origin.x = 0
v1.view.frame = v1frame
var v2frame : CGRect = self.mainScrollView.bounds
v2frame.origin.x = self.view.frame.width
v2.view.frame = v2frame
var v3frame : CGRect = self.mainScrollView.bounds
v3frame.origin.x = self.view.frame.width*2
v3.view.frame = v3frame
//v1.view.translatesAutoresizingMaskIntoConstraints = false
let csH1 = NSLayoutConstraint.constraints(withVisualFormat: "|-[v]-|", options: [.alignAllTop], metrics: [:], views: ["v": v1.view])
let csH2 = NSLayoutConstraint.constraints(withVisualFormat: "|-[v]-|", options: [.alignAllTop], metrics: [:], views: ["v": v2.view])
let csH3 = NSLayoutConstraint.constraints(withVisualFormat: "|-[v]-|", options: [.alignAllTop], metrics: [:], views: ["v": v3.view])
let csB1 = NSLayoutConstraint.constraints(withVisualFormat: "|-[v]-|", options: [.alignAllBottom], metrics: [:], views: ["v": v1.view])
let csB2 = NSLayoutConstraint.constraints(withVisualFormat: "|-[v]-|", options: [.alignAllBottom], metrics: [:], views: ["v": v2.view])
let csB3 = NSLayoutConstraint.constraints(withVisualFormat: "|-[v]-|", options: [.alignAllBottom], metrics: [:], views: ["v": v3.view])
self.mainScrollView.addConstraints(csH1)
self.mainScrollView.addConstraints(csH2)
self.mainScrollView.addConstraints(csH3)
self.mainScrollView.addConstraints(csB1)
self.mainScrollView.addConstraints(csB2)
self.mainScrollView.addConstraints(csB3)
//UI
self.mainScrollView.contentSize = CGSize(width: self.view.frame.width * 3, height: self.view.frame.size.height)
self.mainScrollView.setContentOffset(CGPoint(x: self.view.frame.width, y: 0), animated: false)
答案 0 :(得分:0)
我建议在你的storyboard里面查看控制器添加一个scrollView固定所有约束(前导,尾随,顶部,底部),然后在scrollView中添加子视图。现在根据scrollView固定你的subView约束(前导,尾随,顶部,底部,相等的宽度(应该等于或大于),相等的高度(应该等于或大于)。
对于水平滚动 如果您想要水平滚动然后还要固定宽度约束,则需要避免自动布局错误,但在占位符选中时需要在构建时删除。
用于垂直滚动 如果您想垂直滚动,还要固定高度约束,这是避免自动布局错误所必需的 但在占位符内检查在构建时删除。
注意: - 您可以使用两者或任何一个
之后在subView中添加三个subView并相应地固定其约束。
最后,在三个子视图中加载三个视图控制器。
在这种方法中,维护您不必处理太多代码将更容易。
答案 1 :(得分:0)
您必须将子视图限制为UIScrollView的内容,这意味着您需要添加4个水平约束: