gpt4 book ai didi

swift - 将 func loadview 中声明的 self.view 约束到 View Controller 的部分区域

转载 作者:行者123 更新时间:2023-11-30 10:33:36 27 4
gpt4 key购买 nike

我的代码位于子类 Canvas 下面,用于绘制属性。我试图将 Canvas 限制在 viewdidload 中的特定区域。问题是 func loadView 阻止 viewdidload 中的约束起作用。在调试区域中,它指出无法同时满足约束。

  var canvas = Canvas()

override func loadView() {
self.view = canvas


}

override func viewDidLoad() {
super.viewDidLoad()

canvas.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate ([
canvas.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
canvas.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
canvas.widthAnchor.constraint(equalToConstant: 75),
canvas.heightAnchor.constraint(equalToConstant: 50),
])

}

class Canvas: UIView {}

最佳答案

您是否将 Canvas 添加为 View 的 subview ?您需要在设置约束之前执行此操作:

        canvas.translatesAutoresizingMaskIntoConstraints = false

self.view.addSubview(canvas)

NSLayoutConstraint.activate ([
canvas.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :37.5),
canvas.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 225),
canvas.widthAnchor.constraint(equalToConstant: 75),
canvas.heightAnchor.constraint(equalToConstant: 50),
])

关于swift - 将 func loadview 中声明的 self.view 约束到 View Controller 的部分区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58568021/

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