2017-07-30 31 views
0

我想在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) 
+0

我建議你嘗試可視化調試器。 [Here](http://imgur.com/a/Z9sYK)有兩個截圖: (1)點擊Xcode編輯器下面的這個按鈕 - 它激活你正在運行的應用程序的可視化調試 (2)檢查是否有紫色矩形在Xcode的頂部狀態欄中輸入一個數字(不確定是否被調用)。如果它在那裏,那就意味着你有自動佈局問題。 –

+0

@DaniyarKarbayev沒有錯誤。即使有,爲什麼只有當我重新打開應用程序時纔會發生這種情況? –

回答

0

我會建議你的故事板視圖控制器內添加一個滾動視圖寄託所有的約束條件(前置,頂部,底部),然後添加滾動視圖裏子視圖。現在針對scrollView(引導,尾隨,頂部,底部,等寬(應等於或大於),等高(應該等於或大於)固定您的子視圖約束。滾動 如果你想滾動水平則也固定寬度的限制,這是必需的,以避免自動佈局錯誤,但裏面的佔位符在構建時檢查刪除。

對於垂直滾動 如果你想滾動垂直然後也被固定高度約束,這是必需的,以避免自動佈局錯誤 但內部佔位符檢查在生成時刪除。

注意: - 您可以使用兩個或任何一個

之後,在您的子視圖中添加三個子視圖,並相應地固定其約束。

最後,在三個子視圖中加載您的三個視圖控制器。

在這種方法中,更容易維護您不必處理大量代碼。

0

你必須約束你的子視圖UIScrollView的內容,這意味着你需要添加4度水平的制約:

  1. 最左邊的觀點應該離開邊緣等於上海華
  2. 第二種觀點應該離開邊緣等於第一視圖的右邊緣和右邊緣等於第三視圖的左邊緣。
  3. 最右邊的(第三個)視圖的右邊緣應該等於超視圖。
+0

嘗試過,不起作用。這樣做會因某種原因禁用滾動視圖 –

相關問題