[英]How to set a subview's frame to be its parent's frame in a custom view
我想将自定义视图的框架设置为我的子视图框架,这是一个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];
}
layoutSubviews
将被多次调用,所以你不应该像这样添加子addSubview
。 更好的风格是设置UILabel的属性,就像你的text
属性一样:
@property (nonatomic, strong) UILabel * label;
然后更改layoutSubviews
代码:
- (void)layoutSubviews
{
if(!self.label){
self.label = [[UILabel alloc] initWithFrame:self.frame]; //Use ARC
self.label.textAlignment = NSTextAlignmentCenter;
[self addSubview:self.label];
}
self.label.frame = self.bounds;
if (![self.text isEqualToString:@"?"]) {
self.label.text = self.text;
}
}
尝试使用以下代码。
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 40)]; // set frame as you want
label.backgroundColor = [UIColor yellowColor]; // set color as you want
label.text = @"Hellow";
[yourParentView addSubview:label];
使用此代码
- (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;
[self layoutSubviews:frame];
}
return self;
}
- (void)layoutSubviews:(CGRect)myFrame
{
// This works
// UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 40)];
// This doesn't work
UILabel *label = [[UILabel alloc] initWithFrame:myFrame];
label.text = @"";
if (![self.text isEqualToString:@"?"]) {
label.text = self.text;
label.textAlignment = NSTextAlignmentCenter;
}
[self addSubview:label];
}
您不希望在layoutSubviews
方法中创建视图。 可以多次调用该方法,并且您将多次创建每个视图(使它们彼此重叠)。
而是在initWithFrame
方法中创建所需的视图。
对于实际问题的答案,您可能需要将新标签的框架设置为自定义视图的边界。 边界的原点为0,0因此无需将自定义视图放在其超级视图中。 如果使用框架,标签将偏移自定义视图偏移的程度。
你可以完全删除layoutSubviews
方法,因为它不是设置标签文本的地方。 然后添加:
- (instancetype)initWithFrame:(CGRect)frame withText:(NSString *)text
{
self = [super initWithFrame:frame];
if (!self) return nil;
self.backgroundColor = [UIColor grayColor];
self.layer.cornerRadius = 3;
self.layer.borderWidth = 1;
self.layer.borderColor = [UIColor blackColor].CGColor;
_text = text;
UILabel *label = [[UILabel alloc] initWithFrame:self.bounds];
label.text = @"";
if (![self.text isEqualToString:@"?"]) {
label.text = self.text;
label.textAlignment = NSTextAlignmentCenter;
}
[self addSubview:label];
return self;
}
如果您希望在自定义视图调整大小时调整标签大小,可以通过设置label.frame = self.bounds;
在layoutSubviews中执行此label.frame = self.bounds;
,但你最好使用自动调整大小的掩码或自动布局。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.