我试图使用Xcode游乐场制作东西。当我尝试使用自动布局功能时,出现一条错误消息 类型“ GameView”的值没有成员“ view”
这是我的代码,还有更多代码,但是我只添加了相关的部分。
import Foundation
import UIKit
import SpriteKit
public class GameView : UIView{
self.frame.size.width, height: self.frame.size.height))
override public init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: 1500, height: 1000))
}
public override func addConstraints(_ constraints: [NSLayoutConstraint]) {
self.view.addConstraints([
NSLayoutConstraint(item: GameView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: GameView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 64),
NSLayoutConstraint(item: GameView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: GameView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0),
])
}
}
谢谢!
答案 0 :(得分:2)
使用“自我”而不是“ self.view”。 并且您应该使用GameView.self。
import Foundation
import UIKit
import SpriteKit
public class GameView : UIView{
override public init(frame: CGRect) {
super.init(frame: CGRect(x: 0, y: 0, width: 1500, height: 1000))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
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),
])
}
}
答案 1 :(得分:1)
好吧,它没有view
属性,我想您只想使用self
。您可能从某种UIViewController
复制了此代码。