gpt4 book ai didi

带有 ScrollView 和键盘的 iOS AutoLayout

转载 作者:技术小花猫 更新时间:2023-10-29 10:42:30 28 4
gpt4 key购买 nike

我的 Storyboard上有一个 View 显示用户登录表单,所以它看起来像这样:主视图-> ScrollView ->内容 View ->两个文本字段和登录按钮在顶部,一个注册按钮在 View 的底部。我使用自动布局,底部按钮有底部空间限制。当我点击文本字段并出现键盘时,我想 ScrollView 以将大小更改为可见矩形,但内容大小应保持向下滚动到注册按钮,但当 ScrollView 的大小发生变化时按钮向上移动。我怎么能做我想做的事?

我在出现键盘时使用此代码:

- (void)keyboardWillShow:(NSNotification *)aNotification
{
NSDictionary *info = [aNotification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [kbFrame CGRectValue];

CGSize s = self.scrollView.contentSize;
CGFloat height = keyboardFrame.size.height;
self.scrollViewBottomLayoutConstraint.constant = height;

[UIView animateWithDuration:animationDuration animations:^{
[self.view layoutIfNeeded];
[self.scrollView setContentSize:s];
}];
}

最佳答案

尝试不考虑内容大小,而是调整 ScrollView 的 contentInset 属性。这样您就不必搞乱约束。

- (void)keyboardUp:(NSNotification *)notification
{
NSDictionary *info = [notification userInfo];
CGRect keyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

UIEdgeInsets contentInset = self.scrollView.contentInset;
contentInset.bottom = keyboardRect.size.height;
self.scrollView.contentInset = contentInset;
}

关于带有 ScrollView 和键盘的 iOS AutoLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19736528/

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