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

标签 ios swift xcode

我正在尝试使用 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/

相关文章:

IOS模拟器9.0不运行

android - Xamarin Forms Ui 项目(多平台)

javascript - react native : Getting the position of an element

ios - 自动布局将尝试通过打破约束来恢复

ios - 在 shouldPerformSegueWithIdentifier 之前对 appdelegate 进行 exc_bad_access

c - Swift - 将转义闭包传递给 C API 回调

iphone - Xcode - 创建 csv/电子表格文件

ios - 如果是 iOS 8,则为 UIAlertController,否则为 UIAlertView

ios - 如何使用ios的swift修改现有的xml

ios - Swift IOS Spritekit 根据设备大小缩放 Sprite 大小