I have a basic MacOS app with a toolbar and a view controller. I'm trying to perform some layout without constraints but when I try to position a subview in the top left corner it gets hidden under the toolbar.
Here's an example to demonstrate the problem (this just requires a default MacOS app with a storyboard). I've added -10 to the y position so the label peeks out.
class ViewController: NSViewController {
let label = NSTextField()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(label)
label.stringValue = "My Label"
}
override func viewDidLayout() {
super.viewDidLayout()
label.sizeToFit()
label.frame = CGRect(x: 0,
y: view.bounds.maxY - 10,
width: label.bounds.width,
height: label.bounds.height)
}
}
What am I doing wrong please? Is there a way to tell the toolbar to sit above the view controller's content rather that overlapping it?
Many thanks in advance!
CodePudding user response:
label.frame.y
is the bottom of the label and its height > 10.
label.frame = CGRect(x: 0,
y: view.bounds.maxY - label.frame.height,
width: label.frame.width,
height: label.frame.height)