ios - Swift: Set UIButton Image with CGRect -


i'd set image of uibutton, without creating of assets @ different sizes.

since should grey rectangle, text on top, i'm trying creating cgrect. title displayed desired, , button can clicked region described cgrect, when run app, nothing can seen, except title. tried fix setting tintcolor, no avail.

here's have far.

@iboutlet weak var swapbutton: uibutton!  

in viewdidload():

var rectangle = cgrectmake(0, 0, view.layer.frame.width, 20) uigraphicsbeginimagecontext(rectangle.size) cgcontexttranslatectm(uigraphicsgetcurrentcontext(), rectangle.origin.x, rectangle.origin.y) var rectangle2 = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext()  swapbutton.setbackgroundimage(rectangle2, forstate: .normal) swapbutton.tintcolor = uicolor.blackcolor() 

is there way accomplish this?

try code :

var yourbutton = uibutton(frame:cgrectmake(100, 100, 100, 35))  var rect  = cgrectmake(0, 0, 1, 1) uigraphicsbeginimagecontext(rect.size) var context : cgcontextref  = uigraphicsgetcurrentcontext()  cgcontextsetfillcolorwithcolor(context, uicolor.blackcolor().cgcolor) cgcontextfillrect(context, rect)  var image : uiimage = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext()  yourbutton.setbackgroundimage(image, forstate: uicontrolstate.normal)  self.view.addsubview(yourbutton) 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -