objective c - iOS Custom UIButton not appearing on view -
i'm having issues overriding uibutton class , customising it.
i have block class uses uibutton class:
#import "block.h" @implementation block -(id)initwithwidth:(int)width withheight:(int)height withxpos:(double)x withypos:(double)y{ _width=width; _height=height; _x=x; _y=y; self = [super initwithframe:[self createrect]]; return self; } - (cgrect)createrect{ cgrect background= cgrectmake(_x, _y, _width, _height); cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetrgbfillcolor(context, 1.0, 0.0, 0.0, 1.0); cgcontextsetrgbstrokecolor(context, 1.0, 0.0, 0.0, 1.0); cgcontextfillrect(context, background); return background; } @end all i'm trying add instance of block class view:
#import "viewcontroller.h" #import "block.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; block* b = [[block alloc]initwithwidth:200 withheight:200 withxpos:0 withypos:200]; [self.view addsubview:b]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } @end really confused why doesn't work!
thanks in advance.
where call createrect? not uiview method , never called on own. did mean use drawrect: instead?
also, there reason made custom initializer , didn't override initwithframe:? seemingly accomplishing same thing in needlessly complicated way.
edit: see happening now. calling when calling super initializer. have tried converting drawrect: instead? attempting perform drawing before view on screen.
here example should work fine:
#import "block.h" @implementation block -(id)initwithframe:(cgrect)frame { if(self = [super initwithframe:frame]) { // put customization here, although example doesn't require } return self; } -(void)drawrect:(cgrect)rect { [super drawrect:rect]; cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextsetrgbfillcolor(context, 1.0, 0.0, 0.0, 1.0); cgcontextsetrgbstrokecolor(context, 1.0, 0.0, 0.0, 1.0); cgcontextfillrect(context, rect); } @end
Comments
Post a Comment