gpt4 book ai didi

Swift:以编程方式从另一个 VC 设置 VC 框架

转载 作者:行者123 更新时间:2023-11-30 12:32:05 28 4
gpt4 key购买 nike

我有两个 ViewControllers - SliderFoo。我将 Slider 添加为 Foo subview 并将其框架设置在 Foo VC 中,但是 Slider 的框架没有改变 -> 我可以看到仅应用了 xy 。我应该如何更改代码才能使用 Slider(initWithFrame: CGFrame) 初始化 Slider?或者从 Foo VC 设置 Slider 大小的首选方法是什么?

//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!)
}

}

最佳答案

您的实现似乎存在一些问题。虽然看起来框架没有改变,但实际上是这样,但是您的 View 没有剪切到边界,因此看起来框架保持不变。

有几点需要注意。

首先,如果您要在代码中进行设置和布局,则需要更新 func“viewDidLayoutSubviews”中的框架/位置。 (看起来 dfd 在评论中也提到了这一点)

还要确保您正确实现 View Controller 包含。每当您尝试将一个 View Controller 的 View 添加到另一个 View Controller 的 View 时,您都应该使用 View Controller 包含。以下是该文档的链接: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()
}

}

关于Swift:以编程方式从另一个 VC 设置 VC 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43400358/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com