3

I have seen several questions regarding this topic; however, none of them resolved the issue, and for the life of me I cannot figure out why the constraints are not taking effect. (Maybe because I haven't slept in a while.. I know it's counterproductive).

I am new to IOS development, but I'm hitting it hard and a quick learner. If you could provide an explanation as to why my original code was not working that would be super helpful. Thumbs up to whoever can resolve issue!

Okay, so I'm developing an app that I've actually been working on for quite a while & I did a real sloppy job when I first began. So I'm basically rebuilding the app's code but completely cutting out the Interface Builder (Storyboard). I am trying to pin a UIButton to a UIView locally (I'd rather not declare it globally).

// parentView initialized //

parentView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
[parentView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:parentView];
parentView.tag = 0;

// homeScreenView initialized //

homeScreenView=[[UIView alloc]initWithFrame:CGRectMake(0, homeScreenTitle.frame.size.height, screenWidth, screenHeight-homeScreenTitle.frame.size.height-height)];
[homeScreenView setBackgroundColor:[UIColor greenColor]];
[parentView addSubview:homeScreenView];
homeScreenView.tag = 2;

// chatMenuView initialized //

chatMenuView=[[UIView alloc]initWithFrame:CGRectMake(0, homeScreenTitle.frame.size.height+10, 100, screenHeight-height-10-10-homeScreenTitle.frame.size.height)];
[chatMenuView setBackgroundColor:[UIColor grayColor]];
[parentView addSubview:chatMenuView];
chatMenuView.tag = 3;

// chatMenuButton initialized //

chatMenuButton = [UIButton buttonWithType:UIButtonTypeCustom];
NSString *buttonText = [NSString stringWithFormat:@"CHAT"];
[chatMenuButton setTitle:buttonText forState:UIControlStateNormal];
[parentView addSubview:chatMenuButton];
[chatMenuButton sizeToFit];
    chatMenuButton.center = CGPointMake(0,screenHeight/2);
UIImage *chatIcon = [UIImage imageNamed:@"GrayHouse1.png"];
[chatMenuButton setBackgroundImage:chatIcon forState:(UIControlStateNormal)];
chatMenuButton.tag = 5;

// pinChatButton //

NSLayoutConstraint *pinChat = [NSLayoutConstraint constraintWithItem:chatMenuView
                                                           attribute:NSLayoutAttributeLeading
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:chatMenuButton
                                                           attribute:NSLayoutAttributeTrailing
                                                          multiplier:1
                                                            constant:0];
[self.view addConstraint: pinChat];

I would like to also add that all of this code is in the viewDidLoad method & all of the other views are declared in the header file (as IBOutlets).Basically when I run the code, the UIView leading margin is at position x = 100, and the button is at position x = 0 which is what it's suppose to be at prior to adding constraints which should also move the button to position x = 100.

4
  • You should set translatesAutoresizingMaskIntoConstraints to NO for all views you want to add NSLayoutConstraint. Commented Nov 6, 2015 at 2:38
  • @user3480295 Perfect! Thank you. I knew I saw this online somewhere, but I couldn't find it again. So, whenever you turn this feature off- all of the other frames that were previously set on the object are voided? Commented Nov 6, 2015 at 2:52
  • Those frames won't be voided, they won't change until those views finish layout. Commented Nov 6, 2015 at 3:17
  • I would recommend you try github.com/PureLayout/PureLayout . Alternatively you can install your constraints in IB then link them programatically to IBOutlet NSLayoutConstraint *property in your M file. Commented Nov 6, 2015 at 5:22

1 Answer 1

1

So I'm basically rebuilding the app's code but completely cutting out the Interface Builder (Storyboard)

First off - you're really swimming against the tide here. Everybody uses Interface Builder for basic layout, because visualizing the layout, and seeing the constraints is much easier in a visual editor. You should really save code-generated constraints for when you're trying to do something clever. Not to mention all that code to set the tag, etc, etc.

Having gotten that out of the way, Your constraint doesn't make a whole lot of sense to me - you're constraining the leading space on chatMenuView to be equal to the trailing space of chatMenuButton. I can imagine scenarios in which this'd be useful, but what you probably want for this is something more like:

NSLayoutConstraint *pinChat = [NSLayoutConstraint constraintWithItem:chatMenuButton
                                                       attribute:NSLayoutAttributeLeading
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:chatMenuView
                                                       attribute:NSLayoutAttributeLeading
                                                      multiplier:1
                                                        constant:100];

Finally, even if you are sure you want to create constraints in code, consider using the visual format, which is at least somewhat readable.

Sign up to request clarification or add additional context in comments.

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.