swift - Textured NSWindow Background Gradient -
i trying generate own textured nswindow appears different gradient default metallic appearing one.
so far i've subclassed nswindow no success below.
import cocoa class pswindow: nswindow { override init(contentrect: nsrect, stylemask astyle: int, backing bufferingtype: nsbackingstoretype, defer flag: bool) { super.init(contentrect: contentrect, stylemask: astyle, backing: bufferingtype, defer: flag) let gradient: nsgradient = nsgradient(startingcolor: nscolor(red: 48 / 255, green: 35 / 255, blue: 174 / 255, alpha: 1), endingcolor: nscolor(red: 200 / 255, green: 109 / 255, blue: 215 / 255, alpha: 1)) gradient.drawinrect(contentrect, angle: 45) } }
am going correct way?
nswindow subclass of nsresponder , shouldn't drawing within it.
solutions:
you should override "contentview" variable (custom window subclass) , set layer cagradientlayer create in set method of "contentview"
create custom subclass of nsview , set view of custom window in storyboard/xib. in view either override drawinrect or use layer approach.
additionally here how draw custom nswindow
quick dirty code follows
import cocoa class customwindow : nswindow { override init(contentrect: nsrect, stylemask astyle: int, backing bufferingtype: nsbackingstoretype, defer flag: bool) { super.init(contentrect: contentrect, stylemask: astyle, backing: bufferingtype, defer: flag) } override var contentview: anyobject { set { var view = newvalue as! nsview view.wantslayer = true let colortop = nscolor(red: 48 / 255, green: 35 / 255, blue: 174 / 255, alpha: 1).cgcolor let colorbottom = nscolor(red: 200 / 255, green: 109 / 255, blue: 215 / 255, alpha: 1).cgcolor let gradient = cagradientlayer() gradient.colors = [ colortop, colorbottom] gradient.locations = [ 0.0, 1.0] view.layer = gradient super.contentview = view } { return super.contentview } } required init?(coder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
solution 2:
import cocoa class customview : nsview { override func drawrect(dirtyrect: nsrect) { let colortop = nscolor(red: 48 / 255, green: 35 / 255, blue: 174 / 255, alpha: 1) let colorbottom = nscolor(red: 200 / 255, green: 109 / 255, blue: 215 / 255, alpha: 1) let gradient = nsgradient(colors: [colortop, colorbottom]) gradient.drawinrect(dirtyrect, angle: 45) } }
if need custom titlebar here. it's lot of work.
ps: remember actual drawing done in nsview
Comments
Post a Comment