gpt4 book ai didi

ios - NSLayoutConstraint 导致编程工具栏不显示

转载 作者:行者123 更新时间:2023-11-29 01:55:02 26 4
gpt4 key购买 nike

我有一个程序化的 UIToolBar,它是在我的某个 ViewController 的 viewWillAppear: 方法中创建和绘制的。我有 NSLayoutConstraint 设置以将我的 toolbar 保持在屏幕底部,它适用于 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

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

这是加载后的样子:

最佳答案

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

关于ios - NSLayoutConstraint 导致编程工具栏不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30959908/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com