【问题标题】:Detected missing constraints for NSTextView检测到 NSTextView 缺少约束
【发布时间】:2017-02-01 03:49:33
【问题描述】:

我创建了一个基于文档的应用程序,它应该将带有图像的 NSAttributedString 保存到包中。我运行应用程序并将图像添加到文本视图并保存。当我打开文件时,一个对话框说“无法打开文档'x'”,并打印到控制台:

[Layout] Detected missing constraints for <NSTextField: 0x100b3f480>.  It cannot be placed because there are not enough constraints to fully 
define the size and origin. Add the missing constraints, or set 
translatesAutoresizingMaskIntoConstraints=YES and constraints will be generated for you. If this view is laid out manually on macOS 10.12 and later, 
you may choose to not call [super layout] from your override. Set a breakpoint on DETECTED_MISSING_CONSTRAINTS to debug. This error will only be logged once.

文档.swift:

enum CookRecipesFileNames : String {

    case notes = "Notes.rtfd"
}

class Document: NSDocument {

    var documentFileWrapper = FileWrapper(directoryWithFileWrappers: [:])

    var popover : NSPopover?

    var notes : NSAttributedString = NSAttributedString()

    ...

    override class func autosavesInPlace() -> Bool {
        return true
    }

    override func fileWrapper(ofType typeName: String) throws -> FileWrapper {

        let notesRTFdata = try self.notes.data(from: NSRange(0..<self.notes.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSRTFDTextDocumentType])

        if let oldTextFileWrapper = self.documentFileWrapper.fileWrappers?[CookRecipesFileNames.notes.rawValue] {
            self.documentFileWrapper.removeFileWrapper(oldTextFileWrapper)
        }

        self.documentFileWrapper.addRegularFile(withContents: notesRTFdata, preferredFilename: CookRecipesFileNames.notes.rawValue)

        return self.documentFileWrapper
    }

    override func read(from fileWrapper: FileWrapper, ofType typeName: String) throws {
        guard let documentNotesData = fileWrappers[CookRecipesFileNames.notes.rawValue]?.regularFileContents else {
            throw err(.cannotLoadNotes)
        }

        guard let documentNotes = NSAttributedString(rtfd: documentNotesData, documentAttributes: nil) else {
            throw err(.cannotLoadNotes)
        }

        self.documentFileWrapper = fileWrapper

        self.notes = documentNotes
    }

}

任何帮助将不胜感激!

【问题讨论】:

  • 您是否在 Interface Builder 中或以编程方式设置了任何约束?
  • 在界面生成器中设置它们
  • 好的,我会添加一个答案。
  • @Willeke 不,不重复。那是关于NSOpenPanel。当我打开以前保存的文件(如果其中有任何图像)时会触发我的。

标签: swift cocoa nstextview


【解决方案1】:

警告“检测到缺少约束...”是构建警告,告诉您已向视图添加约束,但不足以确定视图的 x 和 y 坐标以及高度和宽度。如果您在 IB 中未添加任何约束(此处的一个选项是全部删除),Xcode 将告诉应用程序使用 IB 中使用的确切位置和尺寸。如果向视图添加任何约束,则必须添加足够的约束来确定坐标和两个维度。如果您想保留约束(这是第二个选项),请转到 IB,并在左上角查找黄色或红色警告错误:

这将为您提供视图控制器中缺失和冲突的约束列表。

【讨论】:

  • 您是对的,对于我的回答中的错误,我们深表歉意。这是一个构建警告,也是一个常见的警告。 IB中有警告箭头吗?
  • 不,我没有。在此处查看照片:http://i.imgur.com/pLdBGKG.png
  • 哦,这应该是运行时错误,因为我可以成功编译和保存东西,但我无法打开它。如果我不将图像添加到文本字段,我可以成功保存并重新打开文件。因此,我认为我的代码有问题
  • 很抱歉回复延迟,而且我认为我无能为力。我已经看过数百次“缺少约束”和“无法满足约束”的警告,而且对于开发人员而言,NSLayoutConstraints 的使用一直很差。如果你们所有人都在 IB 中,而没有在代码中,那么我不知道是什么原因造成的。我将把这个答案留在这里,以便如果您确定在运行时(即在代码中)添加了约束,我希望能提供帮助。
猜你喜欢
  • 1970-01-01
  • 2018-12-12
  • 2021-06-13
  • 2019-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2018-09-01
相关资源
最近更新 更多