2016-11-05 6 views
0

ViewController displaying UIView with elements outside of their containerUIView элементы, относящиеся к подклассу UIView обнаруживаются за пределами их контейнера

Я просто пытаюсь добавить два UIButton сек и UILabel к подклассу UIView, что я сделал. Вот что подкласс:

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 to a group game by %@", 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 

На мой взгляд, контроллер, я добавляю InvitedView экземпляр:

invitedView = [[InvitedView alloc] initWithFrame:CGRectMake(50, 244, 220, 120)];

Я установил экземпляр спрятан с помощью [invitedView setHidden:YES].

Позже в потоке, мое приложение вызывает метод, который изменяет значение setHidden экземпляра InvitedView так:

- (void)doSomethingWithTheNewValueOfFlagForHid { 
    dispatch_async(dispatch_get_main_queue(), ^(void){ 
     [invitedView setHidden:NO]; 
    }); 
} 

Изображение выше выход. Как вы можете видеть, кнопки и метка не входят в поле, хотя я позиционирую их относительно внутри InsideView.m. Есть идеи? Благодарю.

UPDATE

блок Реализация в InvitedView.m теперь выглядит следующим образом:

@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 to a group game by %@", 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 

ответ

1

Написать выше код, который вы должны писать внутри, если блок initWithFrame: вместо этого писать в

-(void)drawRect:(CGRect)frame{ 
    // Draw your custom view inside this method. 
} 
+0

Привет, Сайд, спасибо за ваш ответ, я обновил свой ответ, чтобы отразить то, что мой код выглядит как реализация вашего решения. Он по-прежнему выглядит на картинке выше. – ewizard

+0

Я понял, это было не из-за drawRect im posting answer – ewizard

0

Мне нужно было изменить:

[gray addSubview:accept]; 
[gray addSubview:decline]; 
[gray addSubview:question]; 

к:

[self addSubview:accept]; 
[self addSubview:decline]; 
[self addSubview:question]; 

Я также использую self вместо gray (удалить gray экземпляров), потому что InvitedView это, само по себе является UIView.