我有两个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