我有兩個ViewControllers
- Slider
& Foo
。 我加入Slider
作爲Foo
subview
並設置它的內部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!)
}
}
的一件事是你的*滑塊的*尺寸* - 希望它在現實中大得多。對於UICollectionView(以及名稱,UISlider),20x20可能太小了。也就是說,你可以爲'Slider'編寫一個* init(frame:)* - 一個Swift有**總是**使用這個,而不是* init(withFrame:)* - 但你可能想要處理Slider它的子視圖佈局* viewDidLoadSubviews *覆蓋。 – dfd