Ho un NSView personalizzato (chiamato Header
) che sono in grado di caricare durante il runtime. Tuttavia, questo NSView personalizzato (Header
) viene incorporato in un'altra NSView personalizzata all'indirizzoil tempo di esecuzione è più grande e produce spazio extra in basso (non voglio questo spazio extra). C'è un altro modo per caricare un file xib che non avrà questo problema?
Ecco come sto caricando il file xib:
import Cocoa
class Header: NSView {
@IBOutlet var view: Header!
@IBOutlet weak var dateLabel: NSTextField!
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
// Drawing code here.
}
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
var topLevelObjects = NSArray()
Bundle.main.loadNibNamed("Header", owner: self, topLevelObjects: &topLevelObjects)
let view = ((topLevelObjects as Array).filter { $0 is NSView })[0] as! NSView
self.frame = NSRect(x: 0, y:0, width: view.frame.width, height: view.frame.height)
self.addSubview(self.view)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
}
risposte:
0 per risposta № 1Perché non aggiungi la tua intestazione usando vincoli e viste. È troppo facile.
let mainContainer = UIView()
mainContainer.backgroundColor = UIColor.blue
mainContainer.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(mainContainer)
let views = ["mainContainer" : mainContainer]
let metrics = ["mainContainerWidth" : self.view.bounds.width]
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[mainContainer]|", options: [], metrics: metrics, views: views))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[mainContainer]|", options: [], metrics: metrics, views: views))
Ecco alcuni esempi di utilizzo di VF.
Spero che questo aiuto.
https://github.com/CristianCardosoA/Auto-layout-VF