gpt4 book ai didi

ios - Xcode 错误 - UIView 中的预期声明

转载 作者:行者123 更新时间:2023-11-29 05:54:15 25 4
gpt4 key购买 nike

我正在尝试使用 Swift Playgrounds 做一些东西,但对自动布局感到困惑。我尝试使用自动布局,但最终出现错误“预期声明”这是我的代码 - 我只添加了相关的部分

import Foundation
import UIKit
import SpriteKit

public class GameView : UIView{

// let GameView : UIView!
override public init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: 1500, height: 1000))

}
GameView.translatesAutoresizingMaskIntoConstraints = false



public override func addConstraints(_ constraints: [NSLayoutConstraint]) {
self.addConstraints([
NSLayoutConstraint(item: GameView.self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: GameView.self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: GameView.self, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: GameView.self, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: 0),
])
}

最佳答案

我认为您正在重写 View 的 addConstraints 函数,但它从未被调用。我认为如果您删除它并将此代码添加到初始化程序的末尾,它应该正确设置约束。

self.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64)
])

(最后两个约束,中心 X 和 Y,没有做任何事情,因为它将 View 的中心 X 约束到自身。另外,我认为您的意思是 self ,其中您有 GameView.self)

关于ios - Xcode 错误 - UIView 中的预期声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317191/

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