gpt4 book ai didi

ios - 当键盘显示不工作时,向上移动包含 UITextField 和 UIButton 的 UIView

转载 作者:行者123 更新时间:2023-11-28 18:00:09 25 4
gpt4 key购买 nike

在主视图中,我有一个 UITableView,我有另一个 View textInputView,它包含一个 UITextField 和一个 UIButton .

当键盘显示不工作时,向上移动包含 UITextFieldUIButtonUIView

-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const int movementDistance = -224; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed

int movement = (up ? movementDistance : -movementDistance);

[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
textInputView.frame = CGRectOffset(textInputView.frame, 0, movement);
[UIView commitAnimations];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}

如果我用 self.view 替换 textInputView,它会向上移动整个 View ,但我不希望这样做。我只想向上移动 textInputView。

这是 View 层次结构:

view heirarchy

这是我的真实看法:

enter image description here

顺便说一句,我正在使用 XCode 6iOS 8 SDK...

最佳答案

由于您使用的是 textInputView 的框架,它在不同的时间是不同的,因此您没有获得预期的效果,

像这样,

[self.view bringSubviewToFront: textInputView]
if(up){
textInputView.frame = CGRectOffset(textInputView.bounds, 0 self.view.bounds.size.height - textInputView.bounds.size.height)
}else{
textInputView.frame = CGRectOffset(textInputView.bounds, 0 self.view.bounds.size.height)
}

现在,我觉得您想在键盘出现时在键盘上方显示输入 View ,并在键盘消失时隐藏它。首选方法是观察 UIKeyboardWillShowNotificationUIKeyboardWillHideNotification。它还提供动画持续时间和最终键盘帧。

- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

}


- (void)keyboardWillShow:(NSNotification*)note{
NSDictionary *userInfo = note.userInfo;
CGRect finalKeyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

float inputViewFinalYPosition = self.view.bounds.size.height - finalKeyboardFrame.size.height;
CGRect inputViewFrame = textInputView.bounds;
inputViewFrame.origin.y = inputViewFinalYPosition;

[UIView animateWithDuration:animationDuration animations:^{
textInputView.frame = inputViewFrame;
}];

}

- (void)keyboardWillHide:(NSNotification*)note{

NSDictionary *userInfo = note.userInfo;
NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

CGRect inputViewFrame = textInputView.bounds;
inputViewFrame.origin.y = self.view.bounds.size.height;

[UIView animateWithDuration:animationDuration animations:^{
textInputView.frame = inputViewFrame;
}];

}

关于ios - 当键盘显示不工作时,向上移动包含 UITextField 和 UIButton 的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25884762/

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