制約を変更する必要がありますが、制約定数を変更することによって 重複が発生します.Xcodeは警告を出します。制約定数を変更する正しい方法は何ですか
1つの制約が古いもので、もう1つが新しいものであることがわかります。 とそのメモリアドレスが異なります。
制約の変更を処理するビューを作成および設定する正しい方法は何ですか?
私の例では、UIViewControllerを拡張するときに、self.viewに提供されたビュー を追加したmainViewがあります。 "self.view"が提供され、制約が設定されていません。
メインのメインビューはコンテナで、キーボードが表示されるたびにキーボードの高さに合わせて高さを調整します。
のviewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
_mainView = [UIView new];
_mainView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_mainView];
}
viewWillLayoutSubviews:
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
//moved to viewDidLoad
//self.mainView.translatesAutoresizingMaskIntoConstraints = NO;
//top
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0]];
//bottom
_mainBottomContraint = [NSLayoutConstraint constraintWithItem:_mainView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:_keyboardOffset];
[[self view] addConstraint:_mainBottomContraint];
//left
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0]];
//right
[[self view] addConstraint:[NSLayoutConstraint constraintWithItem:_mainView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];
-
私は、制約の定数を変更するたびに、私は、重複制約を取得し、 viewWillLayoutSubviewsが再び呼ばれ、再作成されますコンひずみ。 再作成せずに制約をどのように変更しますか?または、ここで使用する正しいパターンは何か。
keyboardWillShow - メモリアドレスに制約(setConstant)
- (void)keyboardWillShow:(NSNotification*)aNotification {
if (![_chatTextField isFirstResponder]) {
return;
}
CGSize tabBarSize = [[[self tabBarController] tabBar] bounds].size;
NSDictionary* info = [aNotification userInfo];
NSTimeInterval duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [[info objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGSize kbSize = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = _mainView.frame;
bkgndRect.size.height -= kbSize.height-tabBarSize.height;
//animate with keyboard
_keyboardOffset = -kbSize.height-tabBarSize.height;
[_mainBottomContraint setConstant:_keyboardOffset];
[self.view setNeedsLayout];
[UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{
[_mainView layoutIfNeeded];
} completion:nil];
}
LOGS
制約1を変更0x174286450
制約2メモリアドレス0x174288660
0123でサブビューの(
"<NSLayoutConstraint:0x174286450 UIView:0x14fd5e5b0.bottom == UIView:0x14fe5edd0.bottom - 271 (active)>",
"<NSLayoutConstraint:0x174288660 UIView:0x14fd5e5b0.bottom == UIView:0x14fe5edd0.bottom (active)>"
)
私はまだ重複を取得しています、私はviewDidLoadを更新して、あなたが言及したようにそれらを追加する前にマスクを制約に加えます。 質問をログで更新します。 – Wayne
あなたの提案で私の質問を更新しました。 – Wayne