为什么我在subView中添加一些约束时会出错?

时间:2018-03-09 02:49:12

标签: ios objective-c constraints

LogoView是top_bg的子视图。 top_bg是窗口的子视图。我尝试使用LogoView添加约束。为什么我会弄错?

 CGRect screenFrame = [[UIScreen mainScreen] bounds];
 UIImageView *logoView = [[UIImageView alloc]init];//logo
UIImage *logoImage = [UIImage imageNamed:@"top_ico.png"];
[logoView setImage:logoImage];//below add constraint
//  logoView.frame = CGRectMake(150.0f,0.0f, 304.74f, 60.0f);
[logoView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *logoConstraint_0 = [NSLayoutConstraint constraintWithItem:logoView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:top_bg attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
NSLayoutConstraint *logoConstraint_1 = [NSLayoutConstraint constraintWithItem:logoView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:top_bg attribute:NSLayoutAttributeLeft multiplier:1.0f constant:screenFrame.size.width/2.0f];
NSLayoutConstraint *logoConstraint_2 = [NSLayoutConstraint constraintWithItem:logoView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:304.74f];
NSLayoutConstraint *logoConstraint_3 = [NSLayoutConstraint constraintWithItem:logoView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:60.0f];
NSArray *LogoConstraints = [NSArray arrayWithObjects:logoConstraint_0,logoConstraint_1,logoConstraint_2,logoConstraint_3,nil];
[top_bg addConstraints:LogoConstraints];
[top_bg addSubview:logoView];

以下是我运行时的错误。

  

2018-03-09 10:43:54.114041 + 0800 MainUI [1855:81910] [LayoutConstraints]视图层次结构没有为约束准备:< NSLayoutConstraint:0x60000009b080 UIImageView:0x7f9196429a20.top == UIImageView:0x7f9196615d70。顶部(非活动)>       添加到视图时,约束的项必须是该视图的后代(或视图本身)。如果在组装视图层次结构之前需要解析约束,则会崩溃。打破 - [UIView(UIConstraintBasedLayout)_viewHierarchyUnpreparedForConstraint:]进行调试。

2 个答案:

答案 0 :(得分:0)

我得到了答案!,如下面的代码

-(instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.top_bg = [[UIImageView alloc]init];
        self.logoView=[[UIImageView alloc]init];
        [self.top_bg addSubview:self.logoView];
    }
    return self;
}

在添加一些约束之前,我必须确认View的层次结构!我无法添加约束,然后添加addSubViews。

答案 1 :(得分:0)

您需要更改代码序列,即首先将logoView添加为子视图,然后添加其约束。

首先添加子视图

$("#myTable").dataTable({
  "columnDefs": [
    { "orderable": false, "targets": 2 }
  ]
});

应用约束

const formatArray = (arr) => {
  let newArr = []
  arr.map((item, index) => newArr.push({'label': index, value: item}))
  return newArr
}

const YourCardComponent = ({data, onPress}) => {
  return (
    <Card>
      <CardSection>
        <Text>{data.content}</Text>
      </CardSection>
      <CardSection>
        <RadioForm
          radio_props={radio_props}
          itemShowKey="label"
          itemRealKey="value"
          dataSource={formatArray(data.selection)}
          onPress={onPress}
          // ... other props as required
        />
      </CardSection>
    </Card>
  )
}

    // Inside the class


_setActive = (active) => this.setState({selected:value})

render () {
  const {details} = this.state
  return (
    <View>
      {details.map((data, index) => (
        <YourCardComponent data={data} key={index} onPress={this._setActive} />
      ))}
    </View>
  )
}