gpt4 book ai didi

ios - 使用NSLayoutConstraint时引起的编译错误

转载 作者:行者123 更新时间:2023-12-02 11:06:50 25 4
gpt4 key购买 nike

当我使用NSLayoutConstraint时,我的代码无法编译。我正在尝试使用代码做所有事情,而不使用 Storyboard 。我不知道这种情况是否正在发生,因为我缺少了一些东西,但它没什么意义,我现在就可以想到为什么它不起作用。该错误发生在judo = line的viewDidLoad函数中。

我收到错误消息:

Cannot assign value of type '[()]' to type '[NSLayout



用于 judo = []部分。
import UIKit

class ViewController: UIViewController {
var Judo1 = [NSLayoutConstraint]()
let FIRE = UIImageView()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(FIRE)
FIRE.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
let trailingC2 = FIRE.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
let topc2 =
FIRE.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
let bottomc2 = FIRE.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true

Judo1 = [leadingc2,trailingC2,topc2,bottomc2]

NSLayoutConstraint.activate(Judo1)
}
}

最佳答案

当您追加

 .active = true

没有约束返回,因此使它们没有 .active = true,因为在这种情况下返回为空
FIRE.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = FIRE.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
let trailingC2 = FIRE.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)
let topc2 = FIRE.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0)
let bottomc2 = FIRE.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0)
judo1 = [leadingc2,trailingC2,topc2,bottomc2]
NSLayoutConstraint.activate(judo1)

使用苹果推荐的更好方法(如果您不需要引用)
NSLayoutConstraint.activate ([

FIRE.topAnchor.constraint(equalTo: view.topAnchor),
FIRE.bottomAnchor.constraint(equalTo: view.bottomAnchor),
FIRE.leadingAnchor.constraint(equalTo: view.leadingAnchor),
FIRE.trailingAnchor.constraint(equalTo: view.trailingAnchor)

])

同样当常数为零时,摆脱它

关于ios - 使用NSLayoutConstraint时引起的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54067396/

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