即使在给出垂直约束之后,各个视图也是重叠的

时间:2016-02-03 14:14:35

标签: ios objective-c constraints

我实际上是想让UIView包含UIImageView和UILabel(并排)。存在多个这样的对。我需要这些对一个在另一个之下。但在第二对之后,尽管给出了垂直约束,但重叠开始了。下面的代码实际上是导致视图重叠(第三和第四个进入第二)我无法确切地知道出了什么问题。感谢有人可以指出这个

    UIView *bottomCaseStudiesLyt = [[UIView alloc]init];
bottomCaseStudiesLyt.translatesAutoresizingMaskIntoConstraints  = NO;
[contentView addSubview:bottomCaseStudiesLyt];

NSLayoutConstraint* bottomCaseStudiesleftConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[contentView addConstraint:bottomCaseStudiesleftConstraint];

NSLayoutConstraint* bottomCaseStudiesTopConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:details3View attribute:NSLayoutAttributeBottom multiplier:1.0f constant:10.0f];
[contentView addConstraint:bottomCaseStudiesTopConstraint];

NSLayoutConstraint* bottomCaseStudiesRightConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[contentView addConstraint:bottomCaseStudiesRightConstraint];

NSLayoutConstraint* bottomCaseStudiesBottomConstraint = [NSLayoutConstraint constraintWithItem:bottomCaseStudiesLyt attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
[contentView addConstraint:bottomCaseStudiesBottomConstraint];

//NSArray  *bottomCaseStudiesVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[headingDetailsView]-[details2View]-[details3View]-[bottomCaseStudiesLyt]|" options:0 metrics:nil views:@{@"headingDetailsView": headingDetailsView,@"details2View": details2View,@"details3View": details3View,@"bottomCaseStudiesLyt": bottomCaseStudiesLyt}];

//[contentView addConstraints:bottomCaseStudiesVConstraints];

UIView *firstView = [[UIView alloc]init];
[firstView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: firstView];

[self addImageAndDetails:bottomCaseStudiesLyt previousview:nil whichimage:@"ic_action_easy" whattext:@"Rediculously easy. Takes less than 30 seconds to build a room and go live" mynewview:firstView];

UIView *secondView = [[UIView alloc]init];
[secondView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: secondView];

[self addImageAndDetails:bottomCaseStudiesLyt previousview:firstView whichimage:@"ic_action_amaze" whattext:@"Engage members with great content, services, offers, polls, notification, quiz and more" mynewview:secondView];

UIView *thirdView = [[UIView alloc]init];
[thirdView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: thirdView];

    [self addImageAndDetails:bottomCaseStudiesLyt previousview:secondView whichimage:@"ic_action_subscribers" whattext:@"Members ? No limit! There is a room for all. Go ahead and promote your room." mynewview:thirdView];

UIView *fourthView = [[UIView alloc]init];
[fourthView setTranslatesAutoresizingMaskIntoConstraints:NO];
[bottomCaseStudiesLyt addSubview: fourthView];

    [self addImageAndDetails:bottomCaseStudiesLyt previousview:thirdView whichimage:@"ic_action_crossplatform" whattext:@"Your room can be accessed from any platform or device." mynewview:fourthView];

   NSArray  *bottomViewVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[firsView]-[secondView]-[thirdView]-[fourthView]|" options:0 metrics:nil views:@{@"firsView": firstView,@"secondView": secondView,@"thirdView": thirdView,@"fourthView": fourthView}];
[bottomCaseStudiesLyt addConstraints:bottomViewVConstraints];

// GetStarted button
self->m_ObjGetStartedBut = [[UIButton alloc]init];

[self->m_ObjGetStartedBut setTitle: [NSString stringWithFormat:@"Get Started"] forState:UIControlStateNormal];
self->m_ObjGetStartedBut.backgroundColor = [UIColor redColor];
[self->m_ObjGetStartedBut setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self->m_ObjGetStartedBut.translatesAutoresizingMaskIntoConstraints = NO;
self->m_ObjGetStartedBut.layer.cornerRadius = 10;
self->m_ObjGetStartedBut.clipsToBounds = YES;
[parentView addSubview:self->m_ObjGetStartedBut];

NSDictionary *myTopViews = @{
                             @"scrollView": self->myScrollView,
                             @"submitButton": self->m_ObjGetStartedBut
                             };

NSArray *myTopVConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]-[submitButton(40)]|" options:0 metrics:nil views:myTopViews];
NSArray *myTopHConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[submitButton]-|" options:0 metrics:nil views:@{@"submitButton": self->m_ObjGetStartedBut}];


[parentView addConstraints:myTopVConstraints];
[parentView addConstraints:myTopHConstraints];

//[self->m_ObjGetStartedBut addTarget:self action:@selector(buttonIsReleased:) forControlEvents: UIControlEventTouchUpInside];
//[self->m_ObjGetStartedBut setTag:1];
    }

    -(UIView *)addImageAndDetails:(UIView *)localparentView previousview:(UIView *)prevView whichimage:(NSString *)imageName whattext:(NSString *)relatedText mynewview:(UIView *)itemView
    {


NSLayoutConstraint* topViewleftConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
[localparentView addConstraint:topViewleftConstraint];


NSLayoutConstraint* topViewRightConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[localparentView addConstraint:topViewRightConstraint];

NSLayoutConstraint* topViewTopConstraint = nil;
if(prevView == nil)
{
    topViewTopConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:localparentView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
    [localparentView addConstraint:topViewTopConstraint];
}
else
{


   //topViewTopConstraint = [NSLayoutConstraint constraintWithItem:itemView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:prevView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
    //[localparentView addConstraint:topViewTopConstraint];

}





UIImage *easyToCreateImg = [UIImage imageNamed:imageName];
UIImageView *easyToCreateImgView = [[UIImageView alloc] initWithImage:easyToCreateImg];

easyToCreateImgView.contentMode = UIViewContentModeScaleToFill;
easyToCreateImgView.translatesAutoresizingMaskIntoConstraints  = NO;
easyToCreateImgView.clipsToBounds = YES;
[itemView addSubview:easyToCreateImgView];

NSLayoutConstraint* easyToCreateImgLeftConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateImgLeftConstraint];

NSLayoutConstraint* easyToCreateImgTopConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTop multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateImgTopConstraint];

NSLayoutConstraint *easyToCreateImgHtConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.0f];
[itemView addConstraint:easyToCreateImgHtConstraint];

NSLayoutConstraint *easyToCreateImgWidConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateImgView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80.0f];
[itemView addConstraint:easyToCreateImgWidConstraint];

UILabel* easyToCreateLblView = [[UILabel alloc]init];
[easyToCreateLblView setText:relatedText];
easyToCreateLblView.numberOfLines = 0;
UIFont *newHeadingViewLblFont = [UIFont fontWithName:@"Arial" size:13];
[easyToCreateLblView setFont:newHeadingViewLblFont];
[easyToCreateLblView setTextColor:[UIColor blackColor]];
easyToCreateLblView.translatesAutoresizingMaskIntoConstraints = NO;

[itemView addSubview:easyToCreateLblView];

NSLayoutConstraint* easyToCreateLblTopConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeCenterYWithinMargins relatedBy:NSLayoutRelationEqual toItem:easyToCreateImgView attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:2.0f];
[itemView addConstraint:easyToCreateLblTopConstraint];

NSLayoutConstraint* easyToCreateLblLeftConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:easyToCreateImgView attribute:NSLayoutAttributeRight multiplier:1.0f constant:2.0f];
 [itemView addConstraint:easyToCreateLblLeftConstraint];

NSLayoutConstraint* easyToCreateLblRightConstraint = [NSLayoutConstraint constraintWithItem:easyToCreateLblView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:itemView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
[itemView addConstraint:easyToCreateLblRightConstraint];

return itemView;
    }

1 个答案:

答案 0 :(得分:0)

我不确定我的解释是对还是错。我观察到的是,当我们给第一个视图赋予宽度和高度约束时,我认为这不会被添加的后续视图所尊重。但是,它似乎确实尊重其他约束(顶部,底部,前导,尾随)。所以,当我开始给其他后续视图提供相同大小的宽度和高度时,这个安排就像预期的那样出现了。欢迎来自社区的想法。