2016-11-04 72 views
0

该结构是一个内部带有标签和文本字段的视图。 我需要在标签中使用不同字体大小的不同设备,并且根据标签的宽度,文本字段的宽度非常灵活。下面UITextFiled和UILabel布局失败,砌体结构失败

代码:

UIView *selectBgView = [[UIView alloc] init]; 
    selectBgView.backgroundColor = [UIColor redColor]; 
    [condiView addSubview:selectBgView]; 
    [selectBgView makeConstraints:^(MASConstraintMaker *make) { 
     make.width.equalTo(@200); 
     make.height.equalTo(@20); 
     make.left.equalTo(@10); 
     make.top.equalTo(@20); 
    }]; 

    UILabel *label = [[UILabel alloc] init]; 
    label.backgroundColor = [UIColor grayColor]; 
    label.font = [UIFont systemFontOfSize:13]; 
    label.text = @"Type :"; 
    [condiView addSubview:label]; 
    [label makeConstraints:^(MASConstraintMaker *make) { 
     make.left.top.bottom.equalTo(selectBgView); 
    }]; 

    UITextField * textField = [[UITextField alloc] init]; 
    textField.borderStyle = UITextBorderStyleRoundedRect; 
     textField.font = [UIFont systemFontOfSize:13]; 
    [condiView addSubview:textField]; 
    [textField makeConstraints:^(MASConstraintMaker *make) { 
     make.right.top.bottom.equalTo(selectBgView); 
     make.left.equalTo(label.right); 
    }]; 

它并不像下面的图片工作。

enter image description here

但是,当我以期交换文本框。有用!

enter image description here

UIView *view = [UIView new]; 
    view.backgroundColor = [UIColor greenColor]; 
    [condiView addSubview:view]; 
    [view makeConstraints:^(MASConstraintMaker *make) { 
     make.right.top.bottom.equalTo(selectBgView); 
     make.left.equalTo(label.right); 
    }]; 

如果有人可以帮助我!任何帮助将不胜感激。

+0

我使用故事板并设置相同的常量,它工作得很好... – Lucifron

+0

代码如何?它工作吗? – Yan

回答

0

您的标签没有指定宽度,所以它只有它的宽度。视图的工作方式不同,因为它们没有适合内容的大小。尝试为文本字段指定一个宽度,或者用偏移量将其固定到右侧。

+0

所以没有宽度限制的标签应该是它所需要的宽度。当标签显示后面有一个文本字段,而不是一个视图。我固定了像'make.right.equalTo(selectBgView)'这样的文本字段,这是不够的吗?感谢您的回答。@ Bill Burgess – Yan