当容器视图控制器动态更改子视图控制器时,布局不起作用

发布于 2025-02-03 10:09:57 字数 10676 浏览 3 评论 0原文

我有一个包含contavewiewController的MainViewController。

单击ChildViewControllera中的按钮时,ContainServiewController开始显示ChildViewControllera,并将其动态切换到ChildViewControllerb:

func showNextViewContoller() {

    let childViewControllerB = ChildViewControllerB()

    container.addViewController(childViewControllerB)
    container.children.first?.remove()  // Remove childViewControllerA
}

<

a a href =“ https://i.sstatic.net/zlimu.png” noreferrer“> “

第二视图控制器(viewControllerb)具有图像视图,我会喜欢在中心展示。因此,我分配了以下约束:

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
   
    NSLayoutConstraint.activate([
        imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.6),
    ])

我遇到的问题是图像视图不是垂直居中的:它比应有的要低。

当我运行该应用程序时,该应用程序contunerveiwcontroller首先显示ChildViewControllerb,然后按预期工作。此问题仅在ChildViewControllera之后动态切换时才发生:

“在此处输入映像”

为了帮助调试,我将以下代码添加到所有三个ViewControllers:

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        print("MainViewController bounds = \(self.view.bounds)")
    }

这给出了一个有趣的打印(在iPhone 13 Mini上运行此码)模拟器):

MainViewController bounds = (0.0, 0.0, 375.0, 812.0) //iPhone 13 mini screen is 375 x 812
ChildViewControllerA bounds = (0.0, 0.0, 375.0,738.0). // 812 - 50 safety margin - 24 titlebar = 738.

现在,在单击按钮并添加了ChildViewControllerb之后发生的开关发生:

ChildViewControllerB bounds = (0.0, 0.0, 375.0, 812.0)

似乎ChildViewControllerb假设全屏幕大小并忽略其父级视图控制器的边界(containerServiewController)。因此,ImageView的Hightanchor基于全屏高度,导致其出现在中间。

因此,我将图像视图上的约束更改为:

NSLayoutConstraint.activate([
        imageView.centerYAnchor.constraint(equalTo: super.view.centerYAnchor),
        imageView.centerXAnchor.constraint(equalTo: super.view.centerXAnchor),
        imageView.heightAnchor.constraint(equalTo: super.view.heightAnchor, multiplier: 0.6),
    ])

接下来,我尝试通过添加任何这些行在上面的showextviewcontroller()函数上以上:

container.children.first?.view.layoutSubviews()

//and

container.children.first?.view.setNeedsLayout()

它们都没有使用后,尝试强制布局更新。

如何获得ChildViewControllerb来尊重ContainerServiewController的界限?

如果有帮助,则图像视图最初只需要位于中心。它最终会附着一个锅,捏和旋转的手势,因此用户可以将其移动到任何地方。

编辑01:

这是我要添加和删除子查看控制器的方式:

extension UIViewController {
    func addViewController(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }
    
    func remove() {
        guard parent != nil else { return }
        willMove(toParent: nil)
        view.removeFromSuperview()
        removeFromParent()
    }
}

编辑02:

在一些评论员的推荐下,我更新了AddViewController()函数

func addViewController(_ child: UIViewController) {
        
        addChild(child)
        view.addSubview(child.view)
        
        child.view.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            child.view.topAnchor.constraint(equalTo: self.view.topAnchor),
            child.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            child.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            child.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        ])
        
        
        child.didMove(toParent: self)
        
    }

:似乎没有用,我遇到了错误,说“无法同时满足约束”。不幸的是,我对如何破译错误消息的知识很少...

编辑03:简化项目:

这是一个简化的项目。有四个文件加上AppDelegate(我不使用故事板):

  • MainViewController
  • ViewControllera
  • ViewControllerb
  • 实用程序
  • AppDelegate

MainViewController:

import UIKit

class MainViewController: UIViewController {
    
    let titleBarView = UIView(frame: .zero)
    let container = UIViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        layout()
    }
    
    func setup() {
        
        titleBarView.backgroundColor = .gray
        view.addSubview(titleBarView)
        
        addViewController(container)
        showViewControllerA()
    }
    
    func layout() {
        titleBarView.translatesAutoresizingMaskIntoConstraints = false
        container.view.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            titleBarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            titleBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            titleBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            titleBarView.heightAnchor.constraint(equalToConstant: 24),

            container.view.topAnchor.constraint(equalTo: titleBarView.bottomAnchor, constant: 0),
            container.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            container.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            container.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }
    
    func showViewControllerA() {
        let viewControllerA = ViewControllerA()
        viewControllerA.delegate = self
        
        container.children.first?.remove()
        container.addViewController(viewControllerA)
    }
    
    func showViewControllerB() {
        let viewControllerB = ViewControllerB()
        
        container.children.first?.remove()
        container.addViewController(viewControllerB)
    }
}

extension MainViewController: ViewControllerADelegate {
    func nextViewController() {
        showViewControllerB()
    }
}

ViewController A:ViewController a:

protocol ViewControllerADelegate: AnyObject {
    func nextViewController()
}

class ViewControllerA: UIViewController {
    
    let nextButton = UIButton()
    
    weak var delegate: ViewControllerADelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        layout()
        view.backgroundColor = .gray
    }
    
    func setup() {
        nextButton.setTitle("next", for: .normal)
        nextButton.addTarget(self, action: #selector(nextButtonPressed), for: .primaryActionTriggered)
        view.addSubview(nextButton)
    }
    
    func layout() {
        nextButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            nextButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc func nextButtonPressed() {
        delegate?.nextViewController()
    }  
}

viewController b:

import import uikit uikit

class ViewControllerB: UIViewController {
    
    let imageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        layout()
    }
    
    func setup() {
        view.addSubview(imageView)
        blankImage()
    }
    
    func layout() {
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        imageView.layer.magnificationFilter = CALayerContentsFilter.nearest;
       

        NSLayoutConstraint.activate([
            imageView.centerYAnchor.constraint(equalTo: super.view.centerYAnchor),
            imageView.centerXAnchor.constraint(equalTo: super.view.centerXAnchor),
            imageView.heightAnchor.constraint(equalTo: super.view.heightAnchor, multiplier: 0.6),
        ])
        
        view.layoutSubviews()

    }
    
    func blankImage() {
        let ciImage = CIImage(cgImage: createBlankCGImage(width: 32, height: 64)!)
        imageView.image = cIImageToUIImage(ciimage: ciImage, context: CIContext())
    }
}

Utilities

import Foundation
import UIKit

func createBlankCGImage(width: Int, height: Int) -> CGImage? {
    
    let bounds = CGRect(x: 0, y:0, width: width, height: height)
    let intWidth = Int(ceil(bounds.width))
    let intHeight = Int(ceil(bounds.height))
    let bitmapContext = CGContext(data: nil,
                                  width: intWidth, height: intHeight,
                                  bitsPerComponent: 8,
                                  bytesPerRow: 0,
                                  space: CGColorSpace(name: CGColorSpace.sRGB)!,
                                  bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)

    if let cgContext = bitmapContext {
        cgContext.saveGState()
        let r = CGFloat.random(in: 0...1)
        let g = CGFloat.random(in: 0...1)
        let b = CGFloat.random(in: 0...1)
        
        cgContext.setFillColor(red: r, green: g, blue: b, alpha: 1)
        cgContext.fill(bounds)
        cgContext.restoreGState()
        
        return cgContext.makeImage()
    }
    
    return nil
}

func cIImageToUIImage(ciimage: CIImage, context: CIContext) -> UIImage? {
    if let cgimg = context.createCGImage(ciimage, from: ciimage.extent) {
        return UIImage(cgImage: cgimg)
    }
    return nil
}


extension UIViewController {
    
    func addViewController(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }
    
    func remove() {
        guard parent != nil else { return }
        willMove(toParent: nil)
        view.removeFromSuperview()
        removeFromParent()
    }
}

:appdelegate:appdelegate:appdelegate:appdelegate:appdelegate ::

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.makeKeyAndVisible()
        
        window?.rootViewController = MainViewController()
        
        return true
    }
}

I have a MainViewController that contains a ContainerViewController.

The ContainerViewController starts out showing childViewControllerA, and dynamically switches it out to childViewControllerB when a button in the childViewControllerA is clicked:

func showNextViewContoller() {

    let childViewControllerB = ChildViewControllerB()

    container.addViewController(childViewControllerB)
    container.children.first?.remove()  // Remove childViewControllerA
}

Here's a diagram:

flow diagram for app

The second view controller (ViewControllerB) has an image view that I'd like to show in the center. So I assigned it the following constraints:

imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFill
   
    NSLayoutConstraint.activate([
        imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.6),
    ])

The problem I'm running into is the imageView is not centered vertically: It's lower than it should be.

When I run the app so that ContainerVeiwController shows childViewControllerB first, then it works as intended. The issue occurs only when childViewControllerB is switched in dynamically after childViewControllerA:

enter image description here

To help debug, I added the following code to all three ViewControllers:

override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        print("MainViewController bounds = \(self.view.bounds)")
    }

And this gave an interesting print out (running this on an iPhone 13 mini simulator):

MainViewController bounds = (0.0, 0.0, 375.0, 812.0) //iPhone 13 mini screen is 375 x 812
ChildViewControllerA bounds = (0.0, 0.0, 375.0,738.0). // 812 - 50 safety margin - 24 titlebar = 738.

Now, the switch happens after the button was clicked and childViewControllerB is added:

ChildViewControllerB bounds = (0.0, 0.0, 375.0, 812.0)

It seems like ChildViewControllerB is assuming a full screen size and ignoring the bounds of it's parent view controller (ContainerViewController). So, the imageView's hightAnchor is based on the full screen height, causing it to appear off center.

So, I changed the constraints on the imageView to:

NSLayoutConstraint.activate([
        imageView.centerYAnchor.constraint(equalTo: super.view.centerYAnchor),
        imageView.centerXAnchor.constraint(equalTo: super.view.centerXAnchor),
        imageView.heightAnchor.constraint(equalTo: super.view.heightAnchor, multiplier: 0.6),
    ])

Next, I tried to force a layout update by adding any of these lines after the switch happens in showNextViewController() function above:

container.children.first?.view.layoutSubviews()

//and

container.children.first?.view.setNeedsLayout()

None of them worked.

How do I get ChildViewControllerB to respect the bounds of ContainerViewController?

If it helps, the imageView only needs to be in the center initially. It'll eventually have a pan, pinch and rotate gesture attached, so the user can move it anywhere they want.

Edit 01:

This is how I'm adding and removing a child view controller:

extension UIViewController {
    func addViewController(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }
    
    func remove() {
        guard parent != nil else { return }
        willMove(toParent: nil)
        view.removeFromSuperview()
        removeFromParent()
    }
}

Edit 02:

On recommendation by a few commentators, I updated the addViewController() function:

func addViewController(_ child: UIViewController) {
        
        addChild(child)
        view.addSubview(child.view)
        
        child.view.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            child.view.topAnchor.constraint(equalTo: self.view.topAnchor),
            child.view.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            child.view.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            child.view.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
        ])
        
        
        child.didMove(toParent: self)
        
    }

This didn't seem to work, I got errors saying "Unable to simultaneously satisfy constraints." Unfortunately I have very little knowledge on how to decipher the error messages...

Edit 03: Simplified Project:

Here's a simplified project. There are four files plus AppDelegate (I'm not using a storyboard):

  • MainViewController
  • ViewControllerA
  • ViewControllerB
  • Utilities
  • AppDelegate

MainViewController:

import UIKit

class MainViewController: UIViewController {
    
    let titleBarView = UIView(frame: .zero)
    let container = UIViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        layout()
    }
    
    func setup() {
        
        titleBarView.backgroundColor = .gray
        view.addSubview(titleBarView)
        
        addViewController(container)
        showViewControllerA()
    }
    
    func layout() {
        titleBarView.translatesAutoresizingMaskIntoConstraints = false
        container.view.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            titleBarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            titleBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            titleBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            titleBarView.heightAnchor.constraint(equalToConstant: 24),

            container.view.topAnchor.constraint(equalTo: titleBarView.bottomAnchor, constant: 0),
            container.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            container.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            container.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }
    
    func showViewControllerA() {
        let viewControllerA = ViewControllerA()
        viewControllerA.delegate = self
        
        container.children.first?.remove()
        container.addViewController(viewControllerA)
    }
    
    func showViewControllerB() {
        let viewControllerB = ViewControllerB()
        
        container.children.first?.remove()
        container.addViewController(viewControllerB)
    }
}

extension MainViewController: ViewControllerADelegate {
    func nextViewController() {
        showViewControllerB()
    }
}

ViewController A:

protocol ViewControllerADelegate: AnyObject {
    func nextViewController()
}

class ViewControllerA: UIViewController {
    
    let nextButton = UIButton()
    
    weak var delegate: ViewControllerADelegate?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        layout()
        view.backgroundColor = .gray
    }
    
    func setup() {
        nextButton.setTitle("next", for: .normal)
        nextButton.addTarget(self, action: #selector(nextButtonPressed), for: .primaryActionTriggered)
        view.addSubview(nextButton)
    }
    
    func layout() {
        nextButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            nextButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            nextButton.centerYAnchor.constraint(equalTo: view.centerYAnchor)
        ])
    }
    
    @objc func nextButtonPressed() {
        delegate?.nextViewController()
    }  
}

ViewController B:

import UIKit

class ViewControllerB: UIViewController {
    
    let imageView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
        layout()
    }
    
    func setup() {
        view.addSubview(imageView)
        blankImage()
    }
    
    func layout() {
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.contentMode = .scaleAspectFill
        imageView.layer.magnificationFilter = CALayerContentsFilter.nearest;
       

        NSLayoutConstraint.activate([
            imageView.centerYAnchor.constraint(equalTo: super.view.centerYAnchor),
            imageView.centerXAnchor.constraint(equalTo: super.view.centerXAnchor),
            imageView.heightAnchor.constraint(equalTo: super.view.heightAnchor, multiplier: 0.6),
        ])
        
        view.layoutSubviews()

    }
    
    func blankImage() {
        let ciImage = CIImage(cgImage: createBlankCGImage(width: 32, height: 64)!)
        imageView.image = cIImageToUIImage(ciimage: ciImage, context: CIContext())
    }
}

Utilities:

import Foundation
import UIKit

func createBlankCGImage(width: Int, height: Int) -> CGImage? {
    
    let bounds = CGRect(x: 0, y:0, width: width, height: height)
    let intWidth = Int(ceil(bounds.width))
    let intHeight = Int(ceil(bounds.height))
    let bitmapContext = CGContext(data: nil,
                                  width: intWidth, height: intHeight,
                                  bitsPerComponent: 8,
                                  bytesPerRow: 0,
                                  space: CGColorSpace(name: CGColorSpace.sRGB)!,
                                  bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)

    if let cgContext = bitmapContext {
        cgContext.saveGState()
        let r = CGFloat.random(in: 0...1)
        let g = CGFloat.random(in: 0...1)
        let b = CGFloat.random(in: 0...1)
        
        cgContext.setFillColor(red: r, green: g, blue: b, alpha: 1)
        cgContext.fill(bounds)
        cgContext.restoreGState()
        
        return cgContext.makeImage()
    }
    
    return nil
}

func cIImageToUIImage(ciimage: CIImage, context: CIContext) -> UIImage? {
    if let cgimg = context.createCGImage(ciimage, from: ciimage.extent) {
        return UIImage(cgImage: cgimg)
    }
    return nil
}


extension UIViewController {
    
    func addViewController(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }
    
    func remove() {
        guard parent != nil else { return }
        willMove(toParent: nil)
        view.removeFromSuperview()
        removeFromParent()
    }
}

AppDelegate:

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.makeKeyAndVisible()
        
        window?.rootViewController = MainViewController()
        
        return true
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霞映澄塘 2025-02-10 10:09:57

小行星在正确的轨道上,但是其他问题……

您没有给子控制器的视图任何约束,因此它们以“本地”大小加载。

建议,更改addViewController(...) func求解ab缺少约束,但是...

如小行星 您的容器 Controller layout()中添加约束,因此您最终会添加约束矛盾的约束。

一种解决方案是将您的addViewController func更改为:

func addViewController(_ child: UIViewController, constrainToSuperview: Bool = true) {
    addChild(child)
    view.addSubview(child.view)
    
    if constrainToSuperview {
        child.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            child.view.topAnchor.constraint(equalTo: view.topAnchor),
            child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }
    
    child.didMove(toParent: self)
}

然后在setup()中:

func setup() {
    
    titleBarView.backgroundColor = .red
    view.addSubview(titleBarView)

    // change this
    //addViewController(container)
    
    // to this
    addViewController(container, constrainToSuperview: false)

    showViewControllerA()
}

在留下其他“添加视图控制器”呼叫时

container.addViewController(viewControllerA)
container.addViewController(viewControllerB)

:将您丢掉的是图像查看约束中的无关 super。:

    NSLayoutConstraint.activate([
        // change this
        //imageView.centerYAnchor.constraint(equalTo: super.view.centerYAnchor),
        //imageView.centerXAnchor.constraint(equalTo: super.view.centerXAnchor),
        //imageView.heightAnchor.constraint(equalTo: super.view.heightAnchor, multiplier: 0.6),

        // to this
        imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.6),
    ])
    

Asteroid is on the right track, but couple other issues...

You are not giving the views of the child controllers any constraints, so they load at their "native" size.

Changing your addViewController(...) func as advised by Asteroid solves the A and B missing constraints, but...

You are calling that same func for your container controller and adding constraints to its view in layout(), so you end up with conflicting constraints.

One solution would be to change your addViewController func to this:

func addViewController(_ child: UIViewController, constrainToSuperview: Bool = true) {
    addChild(child)
    view.addSubview(child.view)
    
    if constrainToSuperview {
        child.view.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            child.view.topAnchor.constraint(equalTo: view.topAnchor),
            child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }
    
    child.didMove(toParent: self)
}

then in setup():

func setup() {
    
    titleBarView.backgroundColor = .red
    view.addSubview(titleBarView)

    // change this
    //addViewController(container)
    
    // to this
    addViewController(container, constrainToSuperview: false)

    showViewControllerA()
}

while leaving your other "add view controller" calls like this:

container.addViewController(viewControllerA)
container.addViewController(viewControllerB)

The other thing that may throw you off is the extraneous super. in your image view constraints:

    NSLayoutConstraint.activate([
        // change this
        //imageView.centerYAnchor.constraint(equalTo: super.view.centerYAnchor),
        //imageView.centerXAnchor.constraint(equalTo: super.view.centerXAnchor),
        //imageView.heightAnchor.constraint(equalTo: super.view.heightAnchor, multiplier: 0.6),

        // to this
        imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
        imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.6),
    ])
    
假装不在乎 2025-02-10 10:09:57

如下:

func addViewController(_ child: UIViewController) {
    addChild(child)
    view.addSubview(child.view)
    child.didMove(toParent: self)
        
    child.view.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        child.view.topAnchor.constraint(equalTo: view.topAnchor),
        child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)])
}
    

mainviewViewController中

func layout() {
    titleBarView.translatesAutoresizingMaskIntoConstraints = false
    container.view.translatesAutoresizingMaskIntoConstraints = false
        
    NSLayoutConstraint.activate([
        titleBarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        titleBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        titleBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        titleBarView.heightAnchor.constraint(equalToConstant: 24),

        //container.view.topAnchor.constraint(equalTo: titleBarView.bottomAnchor, constant: 0),
        //container.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        //container.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        //container.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    ])
        
}

Update as following:

func addViewController(_ child: UIViewController) {
    addChild(child)
    view.addSubview(child.view)
    child.didMove(toParent: self)
        
    child.view.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint.activate([
        child.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        child.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        child.view.topAnchor.constraint(equalTo: view.topAnchor),
        child.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)])
}
    

In MainViewViewController update the layout() function:

func layout() {
    titleBarView.translatesAutoresizingMaskIntoConstraints = false
    container.view.translatesAutoresizingMaskIntoConstraints = false
        
    NSLayoutConstraint.activate([
        titleBarView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        titleBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        titleBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        titleBarView.heightAnchor.constraint(equalToConstant: 24),

        //container.view.topAnchor.constraint(equalTo: titleBarView.bottomAnchor, constant: 0),
        //container.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        //container.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        //container.view.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    ])
        
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文