Я создаю пользовательский класс на основе 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 центрировался на представлении, которое представляет этот класс.
Сбой Xcode при попытке установить ограничения с этим сообщением.
2019-06-26 06: 08: 42.341770 + 0100 Не удалось установить (contentViewController)
определенное пользователем проверенное свойство в (NSWindow): ограничение неправильно
относится к якорям несовместимых типов: