0
我有一個編程UIToolBar是創建和繪製在我的某個ViewController的viewWillAppear:
方法。我有NSLayoutConstraint
設置,以保持我的工具欄在我的屏幕底部,它適用於iPhone 4s和5s。這裏是我的代碼:NSLayoutConstraint導致編程工具欄不顯示
- (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
這的加載/能見度是什麼樣子加載後:
我實際上是使用NSString創建約束格式的全新手段。我不知道如何實際添加到我的'@「V:[commentToolBar(== 44)] - |」',爲什麼我的textView繪製框架時繪製在正確的位置,然後添加爲子視圖到toolBar? – Chisx
「我不確定如何實際添加」您可以在代碼的單獨一行(或多行)中添加更多約束。 – matt
「爲什麼我的textView在繪製框架時繪製在正確的位置,然後作爲子視圖添加到toolBar」因爲文本視圖不受自動佈局的影響。工具欄是。文本視圖按幀定位。工具欄由約束定位。你有正確的框架(或者至少它看起來正好在屏幕的_this_大小上)。你有錯誤的約束。 – matt