【发布时间】:2025-12-03 11:55:02
【问题描述】:
我有一个视图,其中还有一些文本字段。我已经用 self 及其超级视图设置了视图的纵横比。现在,当我单击文本字段时,会出现键盘,我正在调整视图大小并设置其 y 位置,以便键盘不会覆盖文本字段。我正在使用下面的代码。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
/* keyboard is visible, move views */
if([textField isEqual:self.txt_twitter] || [textField isEqual:self.txt_linkedin])
{
[UIView animateWithDuration:0.1f animations:^
{
CGRect rect = self.view.frame;
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= 130;
rect.size.height += 130;
self.view.frame = rect;
}];
}
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
/* resign first responder, hide keyboard, move views */
/* keyboard is visible, move views */
if([textField isEqual:self.txt_twitter] || [textField isEqual:self.txt_linkedin])
{
[UIView animateWithDuration:0.1f animations:^
{
CGRect rect = self.view.frame;
// revert back to the normal state.
rect.origin.y += 130;
rect.size.height -= 130;
self.view.frame = rect;
}];
}
}
现在,当我的视图高度增加时,它的宽度也会增加,因为我已经为此设置了约束。请告诉我如何解决这个问题。在这种情况下,我不想增加视图的宽度。
【问题讨论】:
-
要么完全使用自动布局,要么根本不使用它。您不应该手动编辑由自动布局控制的视图框架。
-
如果对您有帮助,请考虑接受我的回答。
标签: ios autolayout uikit nslayoutconstraint