NSLayoutConstraint导致程序化工具栏无法显示

时间:2015-06-20 23:38:51

标签: ios objective-c nslayoutconstraint

我有一个程序化的 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 按钮添加到工具栏。问题是当它加载时...... 工具栏似乎不存在(它不存在/不可见)而我的按钮根本没有出现,但是我知道 textView 位于正确的位置,因为它出现在底部应该是的位置。难道我做错了什么?

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

的加载/可见性相关的方法

这是加载后的样子:

1 个答案:

答案 0 :(得分:1)

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