gpt4 book ai didi

ios - 当键盘存在时 UIView 编程约束高度增加

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

我正在为应用程序构建评论输入控件。此控件由嵌入 UIView 中的 UITextView 组成。所有约束都以编程方式处理。发生的事情是当用户点击 UITextView 时,键盘将打开。这将调用键盘观察器方法,然后我调整评论输入控件的底部约束以随键盘向上移动。但是,我也在尝试同时增加输入控件的高度,以便用户有更多的输入空间。我很难实现这一点。

-(void)updateViewConstraints
{

NSDictionary *views = @{
@"table" : self.commentsTableView,
@"seeMoreComments" : self.seeMoreCommentsView,
@"commentInput" : self.commentEntryInput
};

//See More Comments Constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[seeMoreComments]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[seeMoreComments(45)]" options:0 metrics:nil views:views]];

//Table view constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[table]-0-|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[seeMoreComments]-0-[table]-0-|" options:0 metrics:nil views:views]];

//Comment entry input
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[commentInput]-0-|" options:0 metrics:nil views:views]];

commentInputVerticalConstraint = [NSLayoutConstraint constraintWithItem:self.commentEntryInput
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:commentInputHeight];

if(commentInputBottomConstraint == nil)
{
commentInputBottomConstraint =
[NSLayoutConstraint constraintWithItem:self.commentEntryInput
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom multiplier:1.0
constant:0.0];

}

[self.view addConstraint:commentInputVerticalConstraint];
[self.view addConstraint:commentInputBottomConstraint];

[super updateViewConstraints];
}

现在我有一个在调用 keyBoardWillShow 时调用的方法。当键盘出现时,此方法会以动画方式显示注释输入控件。

    (void)animateContentWithKeyboardInfo:(NSDictionary *)keyboardInfo
{
NSNumber *animationDuration = keyboardInfo[ UIKeyboardAnimationDurationUserInfoKey ];
NSValue *keyboardFrameValue = keyboardInfo[ UIKeyboardFrameEndUserInfoKey ];
CGRect keyboardFrame = [keyboardFrameValue CGRectValue];
UIViewAnimationCurve animationCurve = [keyboardInfo[ UIKeyboardAnimationCurveUserInfoKey ] intValue];

UIViewAnimationOptions animationOptions = animationOptionWithCurve(animationCurve);

commentInputBottomConstraint.constant = (keyboardFrame.origin.y - [UIScreen mainScreen].bounds.size.height);

//Increase the veritcal height of the comment input control
commentInputVerticalConstraint.constant = 125;

//Takes into account that the Tab Bar is 50 points, and adjust for this
//value.

if(keyboardAppeared == YES)
{
commentInputBottomConstraint.constant += TAB_BAR_OFFSET;
}

else
{
commentInputBottomConstraint.constant -= TAB_BAR_OFFSET;
}

[self.view layoutIfNeeded];

[self.view setNeedsUpdateConstraints];

[UIView animateWithDuration:[animationDuration floatValue] delay:0.0 options:animationOptions animations:
^{


[self.view layoutIfNeeded];

} completion:nil];
}

但是,当我尝试调整 commentInputVerticalConstraint 的常量时,我​​收到此错误消息:

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the `UIView` property `translatesAutoresizingMaskIntoConstraints`) 
(
"<NSLayoutConstraint:0x1899fcb0 V:[CommentEntryInput:0x176e5160(50)]>",
"<NSLayoutConstraint:0x1899e5c0 V:[CommentEntryInput:0x176e5160(125)]>"
)

我不确定是否有办法“重置”或调整约束来处理键盘出现时的情况,然后在键盘消失时将其恢复正常。任何帮助将不胜感激。

最佳答案

您的问题是 -(void)updateViewConstraints 被多次调用。因此,您正在创建一个新约束并将其添加到 View 中两次。尝试检查约束是否为零。

我也不认为在常量的动画变化之前你需要第一个[self.view layoutIfNeeded]。更改常量时,只需设置它,然后将 [self.view layoutIfNeeded] 包装在动画 block 中,以动画显示新值。

关于ios - 当键盘存在时 UIView 编程约束高度增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27728672/

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