2017-04-13 13 views
0

私は2つを持っていますViewControllers - Slider & Foo 私はFoosubviewとしてSliderを追加し、それがFoo VC内部フレームだが、Sliderのフレームは変更されません設定しています - >私はx & yが適用されていることがわかります。 をSlider(initWithFrame: CGFrame)に初期化できるようにするには、コードをどのように変更する必要がありますか?または、Sliderのサイズを設定する好ましい方法は、Foo VCですか?Swift:別のVCからVCフレームをプログラマチックに設定しました

// slider.swift

import UIKit 

class Slider: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 
    var collectionView: UICollectionView? 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
     layout.minimumLineSpacing = 0 
     layout.sectionInset = UIEdgeInsets.zero 
     layout.itemSize = self.view.frame.size 
     layout.scrollDirection = UICollectionViewScrollDirection.horizontal 

     collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) 
     collectionView?.dataSource = self 
     collectionView?.delegate = self 
     collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
     collectionView?.backgroundColor = UIColor.white 
     collectionView?.isPagingEnabled = true 

     self.view.addSubview(collectionView!) 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 14 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 
     cell.backgroundColor = UIColor.red 
     return cell 
    } 
} 

// foo.swift

import UIKit 

class Foo: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     slider = Slider(); 
     slider!.view.frame = CGRect(x: 50, y: 50, width: 20, height: 20) 
     self.view.addSubview(slider!.view) 
     self.addChildViewController(slider!) 
    } 

} 
+0

ことの一つは、あなたの*スライダーの*サイズ* *である - うまくいけば、それは現実にははるかに大きいのです。 20x20は、UICollectionView(および名前、UISlider)にとっては小さすぎる可能性があります。つまり、Swiftが** init(withFrame:)ではなく**これを常に使用している* Sliderの* init(frame:)*をコーディングできますが、おそらく 'Slider' * viewDidLoadSubviews *オーバーライドのサブビューレイアウト。 – dfd

答えて

0

あなたの実装でいくつかの問題を持っているように見えます。フレームが実際には変更されていないように見えますが、ビューが境界にクリッピングされていないので、フレームが同じままであるように見えます。

注意すべきことがいくつかあります。

まず、コードで設定してレイアウトする場合は、 'viewDidLayoutSubviews'機能でフレーム/プレースメントを更新する必要があります。 (dfdのようにコメントにも記載されています)

また、View Controllerの格納を正しく実装していることを確認してください。 1つのビューコントローラのビューを別のビューコントローラのビューに追加しようとするたびに、ビューコントローラの包含を使用する必要があります。ここではそのためのドキュメントへのリンクです:(あなたはそれが主に右だが、あなたの呼び出しが順不同である)

func displayContentController(content: UIViewController) { 
    addChildViewController(content) 
    content.view.frame = frameForContentController() 
    view.addSubview(content.view) 
    content.didMoveToParentViewController(self) 
} 

I:View Controller Containment Docs Here

は、Appleによると、あなたが封じ込めのために実装すべき最低限は次のとおりです。以下のサンプルにUIViewController拡張の例が含まれています。

は、それがテストプロジェクトに私のために働いて、次のコードを試してみてください。私に飛び出し

import UIKit 


class Slider: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { 

var collectionView: UICollectionView? 
var layout : UICollectionViewFlowLayout? 

override func viewDidLoad() { 
    super.viewDidLoad() 

    layout = UICollectionViewFlowLayout() 
    layout?.minimumLineSpacing = 0 
    layout?.sectionInset = UIEdgeInsets.zero 
    layout?.itemSize = view.bounds.size 
    layout?.scrollDirection = UICollectionViewScrollDirection.horizontal 

    collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout!) 
    collectionView?.dataSource = self 
    collectionView?.delegate = self 
    collectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell") 
    collectionView?.backgroundColor = UIColor.blue 
    collectionView?.isPagingEnabled = true 

    self.view.addSubview(collectionView!) 
} 

override func viewDidLayoutSubviews() { 

    // update layout item size here to your new view size 
    layout?.itemSize = view.bounds.size 
    // update collection view frame to new view size here 
    collectionView?.frame = view.bounds 

    super.viewDidLayoutSubviews() 
} 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 14 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) 
    cell.backgroundColor = UIColor.red 
    return cell 
} 
} 


class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    let slider = Slider() 

    // note clips to bounds here, avoids confusion as to where the frame is 
    slider.view.clipsToBounds = true 
    addChild(child: slider, frame: CGRect(x: 50, y: 50, width: 30, height: 30)) 
} 


} 


extension UIViewController { 

// MARK: View Controller Containment convenience functions 

func addChild(child:UIViewController, frame:CGRect) { 
    // notify child of containment 
    child.willMove(toParentViewController: self) 

    // add content as child 
    self.addChildViewController(child) 

    // set frame of child content 
    child.view.frame = frame 

    // add childs view to hierarchy 
    self.view.addSubview(child.view) 

    // call containment delegate 
    child.didMove(toParentViewController: self) 
} 

/* 
* Call this function on a child view controller to remove it from it's parent controller and dismiss it. 
*/ 
func remove(fromParentWithAnimationDuration animationDuration:TimeInterval) 
{ 
    // notify child it's being removed 
    self.willMove(toParentViewController: nil) 

    // remove the view 
    self.view.removeFromSuperview() 

    // remove child controller from container 
    self.removeFromParentViewController() 
} 

} 
+0

ありがとうございます!私は問題がそこに横たわっていた他の 'VC 'をどのように作成したのか、いくつかの問題を発見しました。私の間違いを指摘してくれてありがとう。それでも私はあなたの答えに関するいくつかの質問があります:1)自己を省略することは悪い習慣ですか?あなたがここでやったように - > 'view.bounds' 2)なぜ' frame'の代わりに 'bounds'を使ったのですか? 3) 'func viewDidLayoutSubviews'の最後に' super.viewDidLayoutSubviews() 'を置くのはなぜですか? – Doe

+0

閉鎖時を除き、自己を使用する必要はありません。それは個人的な好みのものです、私は余分な '自己のすべてがなければそれがきれいだと思います。 2)それはあなたが何をしようとしているかによって異なります。ビューフレームはスーパービュー内の配置であり、境界はそれ自身の座標系の配置です。 boundsを使用することにより、サブビューのフレームは毎回0,0、width、heightになります。 3)私はビューをレイアウトしているので、レイアウト変更が完了したらスーパーを呼び出します。私は間違っている可能性がありますが、それはviewDIDLayoutSubviewsなので、superが呼び出されると、すべてのビューがレイアウトされているようです。 – digitalHound

関連する問題