swift 如何以编程方式为我在容器视图中调用的ViewController设置约束?

yi0zb3m4  于 2023-05-16  发布在  Swift
关注(0)|答案(2)|浏览(98)

我已经使用了一个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:********解决了我在单个设备屏幕上遇到的问题,但不能解决所有设备的问题。所以请让我知道一个适当的解决办法

des4xlb0

des4xlb01#

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

controller.view.frame  = // value

2-这个坠毁了

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

因为您应该在设置约束之前添加子视图

self.containerView.addSubview(controller.view)
k4ymrczo

k4ymrczo2#

下面的代码解决了我的问题。

var controller: ViewController!

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

    }

    func setControllerFrame(controller: UIViewController){
        self.view.setNeedsDisplay()
        self.view.setNeedsLayout()

        controller.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            controller.view.topAnchor.constraint(equalTo: self.containerView.topAnchor, constant: 0),
            controller.view.bottomAnchor.constraint(equalTo: self.containerView.bottomAnchor, constant: 0),
            controller.view.leadingAnchor.constraint(equalTo: self.containerView.leadingAnchor, constant: 0),
            controller.view.trailingAnchor.constraint(equalTo: self.containerView.trailingAnchor, constant: 0)
            ])
    }

相关问题