1
我有UItoolbar
其中增加了兩項1. UITextView
和2. UIButton
。 UITextview
的屬性是根據輸入的文本自動生長。 雖然UITextview
高度的增長,我想限制UIButton
高度,即UIButton
高度不應該增加。如何使用NSLayoutConstraint限制UIButton高度
[_toolbar setItems:[NSArray arrayWithObjects:barButtonItemCommentText,barButtonItemSubmit,nil]];
submitBtn.translatesAutoresizingMaskIntoConstraints = NO;
textView.translatesAutoresizingMaskIntoConstraints = NO;
_toolbar.translatesAutoresizingMaskIntoConstraints = NO;
[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-8-[textView]-79-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(textView)]];
[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[textView]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(textView)]];
[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[submitBtn]-8-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(submitBtn)]];
[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-8-[submitBtn]-8-|" options:NSLayoutFormatAlignAllBaseline metrics:nil views:NSDictionaryOfVariableBindings(submitBtn)]];
[_toolbar addConstraint:[NSLayoutConstraint constraintWithItem:submitBtn
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:60]];
[textView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[textView setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[_toolbar setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
[_toolbar addConstraint:[NSLayoutConstraint constraintWithItem:_toolbar attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:MaxToolbarHeight]];
爲了參考,見附圖屏幕截圖:
的高度可能改變這一行'[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@ 「V:| -8- [submitBtn] -8- |」 options:NSLayoutFormatAlignAllBaseline metrics:nil views:NSDictionaryOfVariableBindings(submitBtn)]];'to this'[_toolbar addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@「V:| - > = 8- [submitBtn] -8- |」選項:NSLayoutFormatAlignAllBaseline指標:無視圖:NSDictionaryOfVariableBindings(submitBtn)]];' –
非常感謝@迪瑪,你已經使我的一天:) –