与 UIView 子类相关的 UIView 元素显示在其容器之外

Posted

技术标签:

【中文标题】与 UIView 子类相关的 UIView 元素显示在其容器之外【英文标题】:UIView elements relevant to subclass of UIView are showing up outside of their container 【发布时间】:2016-11-05 19:32:36 【问题描述】:

我只是想将两个UIButtons 和一个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] 将实例设置为隐藏。

稍后在流程中,我的应用调用了一个方法,该方法更改了 InvitedView 实例的 setHidden 值,如下所示:

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

上图是输出。如您所见,按钮和标签不在框内,即使我将它们相对定位在InsideView.m 内。有任何想法吗?谢谢。

更新

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:的if块中,而不是把它写进

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

【讨论】:

嘿 Syed,感谢您的回复,我更新了答案以反映我的代码在实现您的解决方案时的样子。还是和上图一样。 我想通了,不是因为 drawRect 我在发布答案【参考方案2】:

我需要改变:

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

到:

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

我还使用self 代替gray(删除gray 实例化),因为InvitedView 本身就是UIView

【讨论】:

以上是关于与 UIView 子类相关的 UIView 元素显示在其容器之外的主要内容,如果未能解决你的问题,请参考以下文章

iOS UIView 子类将透明文本绘制到背景

UIView 子类的 Swift 通用方法

更新 UIView 子类中 drawRect 中绘制的组件中的文本

自定义 UIView 子类添加 nib 子视图

如何从 Swift 中的 ViewController 获取 UIView 子类实例

iOS——何时创建子 ViewController 与 UIView 子类?