swift - How to move or resize an NSView by setting the frame property? -
i created nsview
in storyboard. in case, nstextfield
. in nsviewcontroller
's viewdidload()
method, want conditionally resize , reposition nstextfield
, setting frame has no effect.
for example:
class viewcontroller: nsviewcontroller { @iboutlet var label: nstextfield! override func viewdidload() { super.viewdidload() // additional setup after loading view. label.frame = nsrect(x: 0, y: 0, width: 200, height: 17) label.setneedsdisplay() } }
when view loads, label still has original frame set in interface builder, , not newly set frame.
how 1 programmatically move/resize label?
the autolayout system culprit here. when set frame, autolayout system overrides re-establish implicit constraints set in storyboard.
set translatesautoresizingmaskintoconstraints
property of label true
. tells autolayout system should create new set of autolayout constraints satisfy new frame you've set:
class viewcontroller: nsviewcontroller { @iboutlet var label: nstextfield! override func viewdidload() { super.viewdidload() // additional setup after loading view. label.frame = nsrect(x: 0, y: 0, width: 200, height: 17) label.translatesautoresizingmaskintoconstraints = true label.setneedsdisplay() } }
Comments
Post a Comment