2016-11-17 69 views
1

我正在處理自定義scrollView,顯示不同類型的不同視圖與分頁。錯誤的框架添加ViewController視圖作爲子視圖

for view in views { 
      view.frame = frame 
      frame.origin.x += width 
     } 
contentViewWidth.constant = width * CGFloat(pages.count) 
self.setNeedsUpdateConstraints() 

適用於任何類型的自定義視圖子類。

現在我必須添加一個視圖控制器爲「頁」,但這樣做:

parentViewController?.addChildViewController(containedViewController) 
     let width = scrollView.frame.size.width 
     let height = scrollView.frame.size.height 
     var frame = CGRect(x: 0, y: 0, width: width, height: height) 
     containedViewController.view.frame = frame 
     pages.append(containedViewController.view) 
     contentView.addSubview(containedViewController.view) 
     containedViewController.didMove(toParentViewController: parentViewController) 

設置它的時候,錯誤檢查完成滾動型我得到一個完全隨機的幀大小我得到正確的框架。 我在7plus,su 414寬度上使用滾動視圖全屏,但我總是得到寬度> 500像素的視圖。

我失蹤了什麼?

回答

1

總是最好添加一個UIView作爲容器視圖並將其設置爲viewController.viewviewController.view.frame由於某些原因容易變得麻煩,特別是在旋轉/動態佈局上......所以請嘗試

parentViewController?.addChildViewController(containedViewController) 

let width = scrollView.frame.size.width 
let height = scrollView.frame.size.height 
let frame = CGRect(x: 0, y: 0, width: width, height: height) 

let containerView = UIView(frame:frame) 

//potentially add flexible width and height 
containedViewController.view.autoresizingMask = [.flexibleWidth,.flexibleHeight] 

containedViewController.view.frame = containerView.frame 

pages.append(containerView) // assuming this is an array of the views inside the scrollview I'd change that to containerView as well.. bit of a guess though. 

containerView.addSubview(containedViewController.view) 
contentView.addSubview(containerView) 
containedViewController.didMove(toParentViewController: parentViewController) 
相關問題