如何在自定义视图中将子视图的框架设置为其父框架

时间:2014-02-18 05:23:23

标签: ios objective-c cocoa-touch uiview subview

我想将自定义视图的帧设置为我的子视图的帧,这是一个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];
}

4 个答案:

答案 0 :(得分:2)

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;
    }
}

答案 1 :(得分:0)

尝试使用以下代码。

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];

答案 2 :(得分:0)

使用此代码

- (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];
}

答案 3 :(得分:0)

您不希望在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中执行此操作,但最好使用自动调整大小的掩码或自动布局。