2016-11-17 4 views
1

私は、ページ作成を使ってさまざまな種類の異なるビューを表示するカスタムのscrollViewに取り組んでいます。フレームがViewControllerを追加して間違っています。サブビューとして表示

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

は、どのような種類のカスタム表示サブクラスでも機能します。

今私は、「ページ」としてのViewControllerを追加する必要がありますが、やって:それを設定するとき

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) 

私はバグが私は完全にランダムなフレームサイズを取得し、完全なscrollviewを検査、右側のフレームを取得します。 私は7plus、su 414 widthでscrollviewフルスクリーンを使用していますが、結果として常に幅が500pxを超えるビューを取得します。

何が欠けていますか?

答えて

1

コンテナビューとしてUIViewを追加し、viewController.viewのフレームをそれに設定することを常に優先します。 viewController.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) 
関連する問題