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

拈花ヽ惹草 提交于 2020-01-17 06:28:09

问题


I am simply trying to add two UIButtons and a UILabel to a subclass of UIView that I made. Here is that 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 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

In my view controller, I add an InvitedView instance:

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

I set the instance to hidden using [invitedView setHidden:YES].

Later on in the flow, my app calls a method which changes the setHidden value of the InvitedView instance like this:

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

The image above is the output. As you can see, the buttons, and label are not within the box, even though I am positioning them relatively inside InsideView.m. Any ideas? Thanks.

UPDATE

Implementation block in InvitedView.m now looks like 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 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:


Write the above code that you have write inside the if block of initWithFrame: instead of this write it into

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



回答2:


I needed to change:

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

to:

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

I also use self in place of gray (remove gray instantiation) because InvitedView is, itself, a UIView.



来源:https://stackoverflow.com/questions/40442370/uiview-elements-relevant-to-subclass-of-uiview-are-showing-up-outside-of-their-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!