ios - UIView elements relevant to subclass of UIView are showing up outside of their container -


viewcontroller displaying uiview elements outside of container

i trying add 2 uibuttons , uilabel subclass of uiview made. here subclass:

invitedview.m

#import "invitedview.h" #import "appdelegate.h"  @class viewcontroller;  @interface invitedview() {     uibutton *accept;     uibutton *decline;     uilabel *question;     uiview *gray;     viewcontroller *myviewcontroller;     nsstring *holduser; }  @end  @implementation invitedview  - (id)initwithframe:(cgrect)frame {     self = [super initwithframe:frame];     if (self) {         gray = [[uiview alloc] initwithframe:frame];          if(![(appdelegate*)[[uiapplication sharedapplication] delegate] invitedby]) {             //         }         else {             holduser = [[nsstring alloc] initwithstring:[(appdelegate*)[[uiapplication sharedapplication] delegate] getinvitedby]];         }          question = [[uilabel alloc] initwithframe:cgrectmake(0, 0, frame.size.width, frame.size.height / 2)];         accept = [[uibutton alloc] initwithframe:cgrectmake(0, frame.size.height / 2, frame.size.width / 2, frame.size.height / 2)];         decline = [[uibutton alloc] initwithframe:cgrectmake(frame.size.width / 2, frame.size.height / 2, frame.size.width / 2, frame.size.height / 2)];          accept.backgroundcolor = [uicolor redcolor];         decline.backgroundcolor = [uicolor greencolor];         question.backgroundcolor = [uicolor bluecolor];         [question setfont:[uifont fontwithname:@"helveticaneue-bold" size:10.0]];         [decline.titlelabel setfont:[uifont fontwithname:@"helveticaneue-bold" size:10.0]];         [accept.titlelabel setfont:[uifont fontwithname:@"helveticaneue-bold" size:10.0]];          [accept.titlelabel settext:@"accept"];         [accept.titlelabel settextcolor:[uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]];         //accept.layer.borderwidth = 2;         //accept.layer.bordercolor = (__bridge cgcolorref _nullable)([uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]);          [decline.titlelabel settext:@"decline"];         [decline.titlelabel setfont:[uifont fontwithname:@"helveticaneue-bold" size:16.0]];         [decline.titlelabel settextcolor:[uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]];         //decline.layer.bordercolor = (__bridge cgcolorref _nullable)([uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]);         //decline.layer.borderwidth = 2;          nslog(@"holduser way down ********: %@", holduser);         [question settext:[[nsstring alloc] initwithformat:@"you have been invited group game %@", holduser]];         question.numberoflines = 0;         question.textalignment = nstextalignmentcenter;         [question settextcolor:[uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]];         [question setfont:[uifont fontwithname:@"helveticaneue-bold" size:14.0]];          [accept addtarget:myviewcontroller action:@selector(acceptinvite) forcontrolevents:uicontroleventtouchupinside];         [decline addtarget:myviewcontroller action:@selector(declineinvite) forcontrolevents:uicontroleventtouchupinside];         [gray addsubview:accept];         [gray addsubview:decline];         [gray addsubview:question];         [self addsubview:gray];     }     return self; }  @end 

in view controller, add invitedview instance:

invitedview = [[invitedview alloc] initwithframe:cgrectmake(50, 244, 220, 120)];

i set instance hidden using [invitedview sethidden:yes].

later on in flow, app calls method changes sethidden value of invitedview instance this:

- (void)dosomethingwiththenewvalueofflagforhid {     dispatch_async(dispatch_get_main_queue(), ^(void){         [invitedview sethidden:no];     }); } 

the image above output. can see, buttons, , label not within box, though positioning them relatively inside insideview.m. ideas? thanks.

update

implementation block in invitedview.m looks this:

@implementation invitedview  -(void)drawrect:(cgrect)frame {      question = [[uilabel alloc] initwithframe:cgrectmake(0, 0, frame.size.width, frame.size.height / 2)];     accept = [[uibutton alloc] initwithframe:cgrectmake(0, frame.size.height / 2, frame.size.width / 2, frame.size.height / 2)];     decline = [[uibutton alloc] initwithframe:cgrectmake(frame.size.width / 2, frame.size.height / 2, frame.size.width / 2, frame.size.height / 2)];      accept.backgroundcolor = [uicolor redcolor];     decline.backgroundcolor = [uicolor greencolor];     question.backgroundcolor = [uicolor bluecolor];     [question setfont:[uifont fontwithname:@"helveticaneue-bold" size:10.0]];     [decline.titlelabel setfont:[uifont fontwithname:@"helveticaneue-bold" size:10.0]];     [accept.titlelabel setfont:[uifont fontwithname:@"helveticaneue-bold" size:10.0]];     [accept.titlelabel settext:@"accept"];     [accept.titlelabel settextcolor:[uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]];     //accept.layer.borderwidth = 2;     //accept.layer.bordercolor = (__bridge cgcolorref _nullable)([uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]);      [decline.titlelabel settext:@"decline"];     [decline.titlelabel setfont:[uifont fontwithname:@"helveticaneue-bold" size:16.0]];     [decline.titlelabel settextcolor:[uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]];     //decline.layer.bordercolor = (__bridge cgcolorref _nullable)([uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]);     //decline.layer.borderwidth = 2;      nslog(@"holduser way down ********: %@", holduser);     [question settext:[[nsstring alloc] initwithformat:@"you have been invited group game %@", holduser]];     question.numberoflines = 0;     question.textalignment = nstextalignmentcenter;     [question settextcolor:[uicolor colorwithred:211.0f/255.0f green:243.0f/255.0f blue:219.0f/255.0f alpha:1.0f]];     [question setfont:[uifont fontwithname:@"helveticaneue-bold" size:14.0]];      [accept addtarget:myviewcontroller action:@selector(acceptinvite) forcontrolevents:uicontroleventtouchupinside];     [decline addtarget:myviewcontroller action:@selector(declineinvite) forcontrolevents:uicontroleventtouchupinside];     [gray addsubview:accept];     [gray addsubview:decline];     [gray addsubview:question]; }  - (id)initwithframe:(cgrect)frame {     if(![(appdelegate*)[[uiapplication sharedapplication] delegate] invitedby]) {         //     }     else {         holduser = [[nsstring alloc] initwithstring:[(appdelegate*)[[uiapplication sharedapplication] delegate] getinvitedby]];     }      self = [super initwithframe:frame];      if (self) {         gray = [[uiview alloc] initwithframe:frame];         [self addsubview:gray];     }     return self; }  @end 

write above code have write inside if block of initwithframe: instead of write

-(void)drawrect:(cgrect)frame{     // draw custom view inside method. } 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -