退出键盘时在iOS中调整视图时我做错了什么 [英] What am I doing wrong to adjust my view in iOS when resigning keyboards
问题描述
我的viewController中有6个文本字段.第一行上为2,第二行为1,第三行为1,最后一行为2.只要输入文本框,文本框就可以正常工作.当键盘出现时,我只是在上下滚动视图时遇到问题.视图可以向上滚动,但是当我辞职"时, 视图向下滚动,但是向下滚动太多. 如果视图向上移动y个值,则一旦我辞职响应者,该视图将向下移动一个大于y的值.
I have 6 Textfields in my viewController. 2 on the first row, 1 on the second, 1 on the third and 2 on the last row. The textfields work fine as far as typing into it. I just have issues scrolling the view up and down when the keyboard appears. The view scrolls up fine but when I "resign responder", the view scrolls down but scrolls down too much. e.g. if the view moved up by y amount, once I resign responder, the view would move down a value greater than y.
我设置了以下代码(以防万一,我提供了额外的代码,但我相信问题出在setViewMovedUp方法中,但我可能是错的):
I have the following code set up (I've provided extra code just in case but I believe the problem is in setViewMovedUp method, but I may be wrong):
键盘显示:
-(void)keyboardWillShow:(NSNotification*)sender {
// Animate the current view out of the way
NSDictionary* info = [sender userInfo];
kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
if (!didViewMove) {
didViewMove = YES;
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
}
-(void)keyboardWillHide {
[UIView animateWithDuration:0.25 animations:^{
self.view.frame = CGRectMake(0, _top, 320, self.view.frame.size.height);
}];
return;
didViewMove = NO;
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
调整视图
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // slide up the view
int desiredHeight = (480 - kbSize.height) / 2;
int requiredHeightAdjustment;
if (didViewMove) {
requiredHeightAdjustment = abs(desiredHeight - (self.view.frame.origin.y + activeField.frame.origin.y));
} else {
requiredHeightAdjustment = abs(desiredHeight - (activeField.frame.origin.y ));
// requiredHeightAdjustment = abs(desiredHeight - (self.view.frame.origin.y + activeField.frame.origin.y));
}
CGRect rect = self.view.frame;
if (movedUp)
{
// 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 -= requiredHeightAdjustment;
rect.size.height += requiredHeightAdjustment;
}
else
{
// revert back to the normal state.
rect.origin.y += requiredHeightAdjustment;
rect.size.height -= requiredHeightAdjustment;
}
self.view.frame = rect;
[UIView commitAnimations];
}
辞职响应者
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([_cardnumbertext isFirstResponder] && [touch view] != _cardnumbertext) {
[_cardnumbertext resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
UITouch *touch1 = [[event allTouches] anyObject];
if ([_salesamounttext isFirstResponder] && [touch1 view] != _salesamounttext) {
[_salesamounttext resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
UITouch *touch2 = [[event allTouches] anyObject];
if ([_referencetext isFirstResponder] && [touch2 view] != _referencetext) {
[_referencetext resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
UITouch *touch3 = [[event allTouches] anyObject];
if ([_datetext isFirstResponder] && [touch3 view] != _datetext) {
[_datetext resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
UITouch *touch4 = [[event allTouches] anyObject];
if ([_timetext isFirstResponder] && [touch4 view] != _timetext) {
[_timetext resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
UITouch *touch5 = [[event allTouches] anyObject];
if ([_reasontext isFirstResponder] && [touch5 view] != _reasontext) {
[_reasontext resignFirstResponder];
}
[super touchesBegan:touches withEvent:event];
}
最后,设置活动字段
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
previousField = activeField;
}
推荐答案
我要做的是使我向上滚动实例变量的最终数量.我还保存了键盘通知中的持续时间和时序曲线设置.
What I do is to make the final amount that I scroll up an instance variable. I also save the duration and timing curve settings from the keyboard notification.
然后,当用户完成编辑并且我收到键盘即将消失的通知时,我可以将视图移动到与最初将视图向上移动的保存量相反的位置.
Then, when the user finishes editing and I get notified that the keyboard is going away, I can just move my view by the reverse of the saved amount that I moved the view up in the first place.
我建议使用这种方法.
我在github上有一个示例项目,详细记录了这种方法:
I have a sample project on github that documents this approach in detail:
在自述文件中查找标题为其他技术"的部分.查找链接滑动视图以为键盘腾出空间".这是使用我上面描述的技术的工作代码.
Look in the README for a section titled "Miscellaneous techniques". Look for a link "Sliding your views to make room for the keyboard". It is working code using the technique I describe above.
这篇关于退出键盘时在iOS中调整视图时我做错了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!