简体   繁体   中英

Creating AutoLayout Constraints programmatically (IOS) Xcode 7.0.1

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM