NSLayoutConstraint causing programmatic toolbar to not show up

127 views Asked by At

I have a programmatic UIToolBar that is created and drawn in the viewWillAppear: method of my certain ViewController. I have NSLayoutConstraint setup to keep my toolbar at the bottom of my screen and it works appropriately for iPhone 4s and 5s. Here is my code:

- (void)viewWillAppear:(BOOL)animated
{
    keyboardSuggestionsOpen = NO;

    //Create custom UIToolBar
    commentToolBar = [[UIToolbar alloc] init];
    commentToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44); //108


    //[self.view removeConstraints:self.view.constraints];
    //Remove all contraints on the toolbar
    for(NSLayoutConstraint *c in self.view.constraints)
        if(c.firstItem == commentToolBar || c.secondItem == commentToolBar)
            [self.view removeConstraint:c];

    [self.view setTranslatesAutoresizingMaskIntoConstraints:YES];
    [self.commentToolBar setTranslatesAutoresizingMaskIntoConstraints:NO];


    commentToolBar.backgroundColor = [UIColor blackColor];
    commentToolBar.tintColor = [UIColor blackColor];
    commentToolBar.barStyle = UIBarStyleBlackOpaque;

    //self.commentToolBar.translatesAutoresizingMaskIntoConstraints = YES;
    //self.commentsContainerView.translatesAutoresizingMaskIntoConstraints = YES;

    [commentTextView setFont:[UIFont systemFontOfSize:22]];
    commentTextView.textAlignment = NSTextAlignmentLeft;
    commentTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, 205, 35)];
    [commentTextView setBackgroundColor:[UIColor whiteColor]];
    [commentTextView.layer setCornerRadius:4.0f];

    UIBarButtonItem *textViewItem = [[UIBarButtonItem alloc] initWithCustomView:commentTextView];

    commentTextView.delegate = self;
    [commentTextView setReturnKeyType:UIReturnKeyDone];

    commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [commentButton setFrame:CGRectMake(0, 0, 75, 35)];
    [commentButton.layer setMasksToBounds:YES];
    [commentButton.layer setCornerRadius:4.0f];
    [commentButton.layer setBorderWidth:0.75f];
    [commentButton.layer setBackgroundColor:[[UIColor colorWithHexString:@"669900"]CGColor]];
    [commentButton setTitle:@"Comment" forState:UIControlStateNormal];
    commentButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [commentButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [commentButton addTarget:self action:@selector(commentButtonInvoked:) forControlEvents:UIControlEventTouchUpInside];

    commentTextView.textColor = [UIColor lightGrayColor];
    commentTextView.text = @"Comment..";

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];

    UIBarButtonItem *commentButtonItem = [[UIBarButtonItem alloc] initWithCustomView:commentButton];
    [commentButtonItem setStyle:UIBarButtonItemStylePlain];

    [self.commentToolBar setItems:[NSArray arrayWithObjects: textViewItem,flexSpace,commentButtonItem, nil] animated:YES];

    [self.view addSubview:commentToolBar];


    NSDictionary* views = NSDictionaryOfVariableBindings(commentToolBar);
    NSString *format = @"V:[commentToolBar(==44)]-|";
    NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                          options:0
                                                          metrics:nil
                                                            views:views];

    [self.view layoutSubviews];

    [self.view addConstraints:constraints];
}

As you can see I just programmatically added a textView and a button to the toolbar. The problem is that when it loads.. the toolbar doesn't even seem to be there(it's absent/invisible) and my button doesn't appear at all, however I know that the textView is in the right location because it appears at the bottom where it should be. Am I doing something wrong?

PS - I have provided a photo and a few more methods that may be pertinent to the loading/visibility of my UIToolBar

This is what it looks like after loading:

1

There are 1 answers

5
matt On BEST ANSWER

The problem is that your constraints on the toolbar are insufficient. You provide its height and its vertical position, but you have said nothing about where it should be horizontally. Thus, it ends up nowhere. Insufficient (ambiguous) constraints very typically come to one's attention when a view simply fails to appear in the interface — and so in your case.