gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:46:26 30 4
gpt4 key购买 nike

我正在使用此代码创建一个基于 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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com