Is there any way to add constraint between a view and the top layout guide in a xib file?

后端 未结 7 2327
时光说笑
时光说笑 2020-12-07 15:44

In iOS 7, we can now add a constraint between a view and the top layout guide, which I think is very useful to solve the status bar offset issue in iOS7(especially when ther

7条回答
  •  Happy的楠姐
    2020-12-07 16:09

    Of course, You can not only add constraint between a view and the top layout guide or bottom layout guide by programmatically but also you can remove and access constraint between a view and the top and bottom layout guide with the help of KVConstraintExtensionsMaster library.

    // create containerView
    UIView *containerView = [UIView prepareNewViewForAutoLayout];
    [containerView setBackgroundColor:[UIColor brownColor]];
    [self.view addSubview:containerView];
    
    // To add Top and Bottom Layout Guide constraint of containerView
    [self applyTopLayoutGuideConstraintToView:containerView withPadding:0];
    [self applyBottomLayoutGuideConstraintToView:containerView withPadding:50];
    
    // To access top Layout Guide Constraint and update it's constant value.
    NSLayoutConstraint *topLayoutGuideConstraint = [self accessAppliedTopLayoutGuideConstraintFromView:containerView];
    topLayoutGuideConstraint.constant = 50;
    
    // To access bottom Layout Guide Constraint and update it's constant value with animation
    NSLayoutConstraint *bottomLayoutGuideConstraint = [self accessAppliedBottomLayoutGuideConstraintFromView:containerView];
    bottomLayoutGuideConstraint.constant = 80;
    [self.view updateModifyConstraintsWithAnimation:NULL]; // call this for animation
    
    // To remove Top and Bottom Layout Guide constraint of containerView
    [self removeAppliedTopLayoutGuideConstraintFromView:containerView];
    [self removeAppliedBottomLayoutGuideConstraintFromView:containerView ];
    

提交回复
热议问题