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

标签 ios objective-c iphone uiview subclass

ViewController displaying UIView with elements outside of their container

我只是想将两个 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

在我的 View Controller 中,我添加了一个 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 中的实现 block 现在如下所示:

@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

最佳答案

initWithFrame: 的 if block 中写入上面的代码,而不是将其写入

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

关于ios - 与 UIView 子类相关的 UIView 元素显示在其容器之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442370/

相关文章:

ios - 多操作系统程序兼容性

ios - socket.IO-objc 示例返回服务器代码 400

ios - 为一个特定的单元格留出余量,UICollectionVIew

ios - 在 iOS 设备上同时记录来自两个路由的输入

iphone - 如何在调用电话后恢复背景音乐(iOS)?

ios - Xcode 9 - 尝试为 UIView 设置动画两次

ios - Avmetadatataitem 在 iOS 上从 iTunes 或 ID3 元数据获取 trackNumber

objective-c - 如何在 Cocoa 中将 NSString 转换为 unsigned int?

iphone - iPhone 上 Objective C 的局限性

iphone - 即使按下取消按钮后如何保留范围栏?