macOS:无法将 NSTextField 置于基于自定义 NSView 的类中

标签 macos cocoa nslayoutconstraint

我正在使用此代码创建一个基于 NSView 的自定义类。

import Foundation
import AppKit

@IBDesignable
class MacBotaoMudaDeCor : NSView {

  lazy var etiqueta: NSTextField = {
    let etiquetax = NSTextField()
    etiquetax.textColor = corLigada


    let xConstraint = NSLayoutConstraint(item: etiquetax,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0)

    let yConstraint = NSLayoutConstraint(item: etiquetax,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0)
    self.addConstraint(xConstraint)
    self.addConstraint(yConstraint)

    etiquetax.integerValue = self.numeroBotao
    etiquetax.sizeToFit()

    self.addSubview(etiquetax)
    return etiquetax
  }()


  // creates a CALayer() and sets it to the layer property of the view
  var mainLayer: CALayer{
    get{
      if layer == nil{
        layer = CALayer()
      }
      return layer!
    }
  }

  @IBInspectable var cornerRadius: CGFloat = 0 {
    didSet {
      mainLayer.cornerRadius = cornerRadius
      mainLayer.masksToBounds = cornerRadius > 0
    }
  }

  @IBInspectable var borderWidth: CGFloat = 0 {
    didSet {
      mainLayer.borderWidth = borderWidth
    }
  }

  @IBInspectable var borderColor: NSColor? {
    didSet {
      mainLayer.borderColor = borderColor?.cgColor
    }
  }

  @IBInspectable var corDesligada: NSColor = NSColor.white
  @IBInspectable var corLigada: NSColor = NSColor.black


  @IBInspectable var ligado: Bool = false {
    didSet {
      self.ajustar(corFundo: ligado ? self.corLigada : self.corDesligada)
    }
  }

  @IBInspectable var numeroBotao: Int = 0 {
    didSet {
      etiqueta.integerValue = numeroBotao
    }
  }


  override init(frame frameRect: NSRect) {
    super.init(frame: frameRect)
    self.ajustar(corFundo: corDesligada)
    self.ajustar(corEtiqueta: corLigada)
  }

  required init?(coder decoder: NSCoder) {
    super.init(coder: decoder)
    self.ajustar(corFundo: corDesligada)
    self.ajustar(corEtiqueta: corLigada)
  }

  override func awakeFromNib() {
    self.ajustar(corFundo: corDesligada)
    self.ajustar(corEtiqueta: corLigada)
  }


  func ajustar(corFundo:NSColor) {
    mainLayer.backgroundColor = corFundo.cgColor
  }


  func ajustar(corEtiqueta:NSColor) {
    etiqueta.textColor = corLigada
  }

}

这个想法是让一个 NSTextField 以此类代表的 View 为中心。

Xcode 在尝试安装带有此消息的约束时崩溃。

2019-06-26 06:08:42.341770+0100 Failed to set (contentViewController) user defined inspected property on (NSWindow): Constraint improperly relates anchors of incompatible types:

最佳答案

addConstraint(_:):

The constraint must involve only views that are within scope of the receiving view.

在添加约束之前添加 View 。

关于macOS:无法将 NSTextField 置于基于自定义 NSView 的类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765599/

相关文章:

ruby - gem install nokogiri -v '1.6.3.1' on OS X 10.10 Yosemite 未安装

安装 OSX 10.11 El Capitan 公共(public) Beta 后 Android SDK 管理器卡住

macos - 如何在 OSX 中从命令行启动 GUI Emacs?

macos - 有关Cocoa应用程序的MVC设计架构的问题

ios - 这是运行通用方法的正确方法吗?

objective-c - 捕获隐藏的 NSWindow

objective-c - 在 OS X 中实现 View 之间滑动的最佳方法是什么?

ios - 以编程方式约束彼此相邻的 3 个 View

ios - UITableView setEditing 不会为标签约束更改设置动画

ios - 编程约束 - 使用布局 anchor 覆盖整个父 View