gpt4 book ai didi

swift - 如何以编程方式对我在容器 View 中调用的 ViewController 施加约束?

转载 作者:行者123 更新时间:2023-11-30 10:39:50 25 4
gpt4 key购买 nike

我对三个具有滚动类型的 Controller 使用了PageViewController。现在我在 containerView 中调用了这个 PageViewController,这里的问题是容器高度小于 PageViewController 的高度,我能够在 containerView 中调用 PageViewController 但底部正在退出视野。我尝试以编程方式放置自动布局,但它不起作用。请告诉我如何解决这个问题?

@IBOutlet weak var containerView: UIView!


override func viewDidLoad() {
super.viewDidLoad()
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ViewController")
self.setControllerFrame(controller: controller )
addChild(controller)
self.containerView.addSubview(controller.view)

}

func setControllerFrame(controller: UIViewController){
//Validating frames according to orientation
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
//*******temp fix:******** this is solutions for a single device not for all devices(screen sizes).
if (self.view.frame.size.width < self.view.frame.size.height){
controller.view.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
} else if (self.view.frame.size.width >= self.view.frame.size.height){
controller.view.frame = CGRect.init(x: 0, y: 0, width: self.view.frame.size.height, height: self.view.frame.size.width)
}
//tried this but crashes at last line.
controller.view.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1, constant: 0)
let leadingConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1, constant: 0)
let trailingConstraint = NSLayoutConstraint(item: controller.view!, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: self.containerView, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1, constant: 0)
view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])
}

上面的代码********temp fix:********解决了我针对单个设备屏幕的问题,但不适用于所有设备。所以请让我知道一个正确的解决方案

最佳答案

1- viewDidLoad 内部 frame 尚不正确,因此将其设置在 viewDidLayoutSubviews

内部
controller.view.frame  = // value

2-崩溃了

view.addConstraints([topConstraint, bottomConstraint, leadingConstraint, trailingConstraint])

因为您应该在设置约束之前添加 subview

self.containerView.addSubview(controller.view)

关于swift - 如何以编程方式对我在容器 View 中调用的 ViewController 施加约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57092536/

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