我有两个TToolButton
- ViewControllers
& Slider
。
我将Foo
添加为Slider
Foo
并在subview
VC中设置其框架,但Foo
的框架不会更改 - >我只能看到Slider
& x
已应用。我应该如何更改代码才能使用y
初始化Slider
?或者,从Slider(initWithFrame: CGFrame)
VC设置Slider
的大小的首选方法是什么?
// slider.swift
Foo
// foo.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
}
}
答案 0 :(得分:0)
您的实施看起来有些问题。虽然看起来框架实际上并没有改变,但是你的视图不是剪切边界,所以看起来框架保持不变。
有几点需要注意。
首先,如果您要在代码中进行设置和布局,则需要在func'viewDidLayoutSubviews'中更新帧/位置。 (看起来dfd也在评论中提到了这一点)
还要确保正确实现视图控制器包含。每当您尝试将一个视图控制器的视图添加到另一个视图控制器的视图时,您应该使用视图控制器包含。以下是该文档的链接:View Controller Containment Docs Here
根据Apple的说法,您应该实施最低限度的遏制措施如下:(您大部分时间都是正确的,但您的通话无序)
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()
}
}