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