[英]UIView elements relevant to subclass of UIView are showing up outside of their container
I am simply trying to add two UIButton
s and a UILabel
to a subclass of UIView that I made. 我只是想将两个
UIButton
和一个UILabel
添加到我制作的UIView的子类中。 Here is that subclass: 这是该子类:
InvitedView.m 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 = [[InvitedView alloc] initWithFrame:CGRectMake(50, 244, 220, 120)];
I set the instance to hidden using [invitedView setHidden:YES]
. 我使用
[invitedView setHidden:YES]
将实例设置为隐藏。
Later on in the flow, my app calls a method which changes the setHidden
value of the InvitedView
instance like this: 稍后,在我的应用程序中,我的应用程序调用了一个更改
InvitedView
实例的setHidden
值的方法,如下所示:
- (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
. 如您所见,即使我将按钮和标签放置在
InsideView.m
相对内部,它们也不在框中。 Any ideas? 有任何想法吗? Thanks.
谢谢。
UPDATE 更新
Implementation block in InvitedView.m now looks like this: 现在, 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
Write the above code that you have write inside the if block of initWithFrame:
instead of this write it into 将上面编写的代码写入
initWithFrame:
的if块中initWithFrame:
而不是将其写入
-(void)drawRect:(CGRect)frame{
// Draw your custom view inside this method.
}
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
. 我还使用
self
代替了gray
(去除gray
实例化),因为InvitedView
本身就是UIView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.