簡體   English   中英

如何在自定義視圖中將子視圖的框架設置為其父框架

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM