Cocoa Custom Control不可见

时间:2017-08-12 22:06:24

标签: swift macos cocoa custom-controls

我尝试在Cocoa中定义一个自定义控件,如下所示

@IBDesignable
class MyCustomContol: NSControl
{ var textfield: NSTextField!

  required public init?(coder: NSCoder)
  { super.init(coder: coder)

    textfield = NSTextField(frame: self.frame)  
    textfield.backgroundColor = NSColor.yellow
    textfield.stringValue="DEFAULT"
    self.addSubview(textfield)
  }
}  

在Interfacebuilder中,我添加了一个自定义视图,并将其类设置为我的类“MyCustomContol”

控件不会出现在Interfacebuilder中,也不会出现在运行时。

出了什么问题。

2 个答案:

答案 0 :(得分:1)

文本字段在IB中不可见,您只能看到NSView。运行时使用与自定义控件相同的框架添加文本字段。您没有看到它,因为文本字段在自定义控件之外。将文本字段的框架(超视图中的坐标)设置为控件的边界(视图中的坐标)。

答案 1 :(得分:0)

tnx @Willeke

使用:

textfield = NSTextField(frame: NSRect(x: 0, y: 0, width: self.frame.width, height: self.frame.height) )

它有效

相关问题