NSLayoutConstraint 导致编程工具栏不显示

NSLayoutConstraint causing programmatic toolbar to not show up

我有一个程序化的 UIToolBar,它是在我的 ViewController 的 viewWillAppear: 方法中创建和绘制的。我有 NSLayoutConstraint 设置以将我的 工具栏 保持在屏幕底部,它适用于 iPhone 4s 和 5s。这是我的代码:

- (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];
}

如您所见,我只是以编程方式添加了一个 textView 和一个 buttontoolbar.问题是当它加载时......工具栏似乎甚至不存在(它是absent/invisible)和我的按钮根本没有出现,但是我知道 textView 位于正确的位置,因为它出现在底部应该出现的位置。难道我做错了什么?

PS - 我提供了一张照片和一些可能与我的 UIToolBar

的 loading/visibility 有关的方法

这是加载后的样子:

问题是你对工具栏的约束不够。您提供了它的高度和垂直位置,但您没有说明它应该水平放置的位置。因此,它最终无处可去。当视图根本无法出现在界面中时,不充分(不明确的)约束通常会引起人们的注意 - 在您的情况下也是如此。