1

I have a view like below which has auto layout set in xibs. enter image description here

Now I need to add a error notification, which will animate from top and the existing views will shift down, like this image below.

enter image description here

I know I can do this easily by simply adding this error view in my existing view controller and managing its height constraints. But I have a couple of other views where I need to re-use this error view. So, now I have created a custom view for this error view. Now my main problem is adding this to my mainview programatically with autolayout. So I need to add the blue error view in my self.view and remove the top layout constraint of the green view and set its top to the blue error view. Make sense? Below is my code for error view and adding it to self.view. But even that doesn't work, am I doing anything wrong here. Any help is appreciated.

-(id)initWithdelegate:(id)parentSelf ForView:(UIView *)parentView
{
    if (self = [super initWithFrame:CGRectMake(0, 0, 100, 100)])
    {

        // Initialization code
        self=(ErrorView*)[[[NSBundle mainBundle] loadNibNamed:@"ErrorView" owner:nil options:nil]objectAtIndex:0];

        self.delegate=parentSelf;

        [parentView addSubview:self];
        self.hidden = YES;
        [self setConstraintsToParentView:parentView];

    }

    return self;
}

-(void)setConstraintsToParentView:(UIView *)parentView
{

    [parentView setTranslatesAutoresizingMaskIntoConstraints:NO];

    //Setting width equal to parentview
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:parentView
                                                          attribute:NSLayoutAttributeWidth
                                                         multiplier:1
                                                           constant:0]];

    //Setting fixed height of 50
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                         attribute:NSLayoutAttributeHeight
                                                         relatedBy:NSLayoutRelationGreaterThanOrEqual
                                                            toItem:nil
                                                         attribute:NSLayoutAttributeNotAnAttribute
                                                        multiplier:1.0
                                                          constant:50]];

    //Setting x pos to center
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:parentView
                                                          attribute:NSLayoutAttributeCenterX
                                                         multiplier:1.0
                                                           constant:0.0]];

    //Setting top position to self.view with constant 20
    [parentView addConstraint:[NSLayoutConstraint constraintWithItem:self
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:parentView
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:20]];

}

I'm calling the errorView like this from my viewcontroller

myErrorView = [[ErrorView alloc]initWithdelegate:self ForView:self.view];
6
  • How exactly does it "not work"? Commented Sep 12, 2016 at 6:24
  • At the moment it's giving me error, Unable to simultaneously satisfy constraints. Commented Sep 12, 2016 at 6:26
  • which constraints? Commented Sep 12, 2016 at 7:10
  • It seems to be giving error on all constraints. For eg: Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7c131ec0 ErrorView:0x7bf8ea30.width == UIView:0x7c035430.width> Commented Sep 12, 2016 at 7:13
  • I made a mistake here, the autoresize property should be added to the error View [self setTranslatesAutoresizingMaskIntoConstraints:NO]; This fixed the autolayout errors. Now the errorView gets added to the main view, now the problem is to add the green view below it. Any ideas. Commented Sep 12, 2016 at 7:37

2 Answers 2

1

you can use identifier propertiy of constraints

NSArray *ArrayConstraint  = yorlableorView.constraints;
 for (NSLayoutConstraint *ob in ArrayConstraint)
      {

                    if ([ob.identifier isEqualToString:@"yourIdentifier"]) 
                   {
                        ob.constant = 0 ;
                // set your constant you want
                    }
            }
Sign up to request clarification or add additional context in comments.

Comments

1

Could you just add the error view as the top view with a height of 0 and then change the height constraint constant when you want to display it? You can animate this and it will simulate it pushing everything down, however with this approach it won't look like it is coming down from the top of the screen.

If you're happy with this approach you could just add a container view to your view with the buttons and tableview, then after you initialise the error view from the xib just assign it to the container, this will save you having to pass the parent view, adding it as a subview and adding the constraints programmatically in your error view class.

Let me know if need more detail or a concrete example. Hope this helps, good luck. Also just as an aside, you could look into Masonry, it makes adding constraints programmatically much easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.