【发布时间】:2014-02-18 05:23:23
【问题描述】:
我想将自定义视图的框架设置为我的子视图的框架,它是一个 UILabel。但
当我使用 self.frame 时,UILabel 显示空文本。我需要硬编码CGRectMake 中的参数才能看到我的UILabel。如何将 UILabel 的框架设置为我的自定义视图的框架?
- (id)initWithFrame:(CGRect)frame withText:(NSString *)text
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor grayColor];
self.layer.cornerRadius = 3;
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor].CGColor;
_text = text;
}
return self;
}
- (void)layoutSubviews
{
// This works
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
// This doesn't work
UILabel *label = [[UILabel alloc] initWithFrame:self.frame];
label.text = @"";
if (![self.text isEqualToString:@"?"]) {
label.text = self.text;
label.textAlignment = NSTextAlignmentCenter;
}
[self addSubview:label];
}
【问题讨论】:
标签: ios objective-c cocoa-touch uiview subview