我想在scrollview(snapchat樣式)中添加3個視圖控制器。下面的代碼似乎工作正常,但是當我退出應用程序(轉到主屏幕),然後回來,意見調整了一些奇怪的原因。我嘗試添加alignAllTop和alignAllBottom,但沒有成功。任何想法爲什麼會發生這種情況?在scrollview中嵌入3個視圖控制器
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)
我建議你嘗試可視化調試器。 [Here](http://imgur.com/a/Z9sYK)有兩個截圖: (1)點擊Xcode編輯器下面的這個按鈕 - 它激活你正在運行的應用程序的可視化調試 (2)檢查是否有紫色矩形在Xcode的頂部狀態欄中輸入一個數字(不確定是否被調用)。如果它在那裏,那就意味着你有自動佈局問題。 –
@DaniyarKarbayev沒有錯誤。即使有,爲什麼只有當我重新打開應用程序時纔會發生這種情況? –