2017-04-13 42 views
0

我有兩個ViewControllers - Slider & Foo。 我加入Slider作爲Foosubview並設置它的內部Foo VC框架,但Slider的框架不會改變 - >我可以看到,只有x & y應用。我應該如何更改我的代碼以便能夠用Slider(initWithFrame: CGFrame)初始化Slider?或者從Foo VC設置大小Slider的首選方式是什麼?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

的一件事是你的*滑塊的*尺寸* - 希望它在現實中大得多。對於UICollectionView(以及名稱,UISlider),20x20可能太小了。也就是說,你可以爲'Slider'編寫一個* init(frame:)* - 一個Swift有**總是**使用這個,而不是* init(withFrame:)* - 但你可能想要處理Slider它的子視圖佈局* viewDidLoadSubviews *覆蓋。 – dfd

回答

0

它看起來像你有你的執行的幾個問題。雖然它看起來像框架沒有改變它實際上是,但你的觀點不剪裁到界限,所以看起來框架保持不變。

一些事情要注意。首先,如果您要設置代碼並進行佈局,則需要在func'viewDidLayoutSubviews'中更新幀/佈局。 (看起來像dfd在評論中也提到了這一點)

還要確保你正確地實現了視圖控制器遏制。無論何時您嘗試將一個視圖控制器的視圖添加到另一視圖控制器的視圖中,都應該使用視圖控制器包含。下面是該文檔的鏈接爲:View Controller Containment Docs Here

據蘋果公司應該實施遏制最低如下:(你得到它基本上是正確的,但你的電話是亂序)

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

我在下面的示例中包含了一個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)爲什麼你使用'bounds'而不是'frame'? 3)爲什麼在'func viewDidLayoutSubviews'的末尾放置'super.viewDidLayoutSubviews()'? – Doe

+0

除了在關閉狀態下,使用自我並不是必需的。這更多的是個人喜好,我認爲它沒有所有額外的'自我'就更清潔。 2)這取決於你要做什麼。視圖框架是它在超視圖中的位置,而界限是它在自己的座標系中的位置。通過使用邊界,每次子視圖的框架將是0,0,寬度,高度。 3)由於我正在佈置視圖,因此當我的佈局更改完成後,我會調用超級視圖。我可能是錯的,但是因爲它是viewDIDLayoutSubviews,所以在調用super的時候應該放棄所有的視圖。 – digitalHound