0

我在 ipad 上有一个表格,最后有很多文本字段和一个按钮。当它处于活动状态时,键盘下方有一些字段。为了使键盘后面的隐藏 texfield 可见,我正在使用以下代码。

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

- (void)textFieldDidEndEditing:(UITextField *)textField
{
_scrollView.frame=CGRectMake(0, 0, 1024, 655);
[self animateTextField:textField up:NO];
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
{
    // NSLog(@"portrait");
    if(up)
    {
        int moveUpValue = temp.y+textField.frame.size.height;
        animatedDis = 264-(1024-moveUpValue-5);
    }
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
    if(up)
    {
        int moveUpValue = 1004-temp.y+textField.frame.size.height;
        animatedDis = 264-(1004-moveUpValue-5);
    }
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
    if(up)
    {
        int moveUpValue = temp.x+textField.frame.size.height;
        animatedDis = 352-(768-moveUpValue-5);
    }
}
else
{
    if(up)
    {
        int moveUpValue = 768-temp.x+textField.frame.size.height;
        animatedDis = 352-(768-moveUpValue-5);
        _scrollView.frame = CGRectMake(0, 0, 1024, 655-240);
    }

}
if(animatedDis>0)
{
    const int movementDistance = animatedDis;
    const float movementDuration = 0.3f;
    int movement = (up ? -movementDistance : movementDistance);
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    if (orientation == UIInterfaceOrientationPortrait)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else if(orientation == UIInterfaceOrientationLandscapeLeft)
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    else
    {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    }
    [UIView commitAnimations];
}
}

我也在使用滚动视图。我的问题是,当键盘处于活动状态并且我按下按钮时,它会将我带到下一个屏幕。现在,如果我向后导航,则键盘处于活动状态并设置了先前的动画。现在,如果我隐藏键盘,整个视图会向下滚动,并在顶部留下黑色部分。如何处理这种情况?

4

1 回答 1

0

好的。经过大量研究,我找到了一个简单的方法。这是通知的使用。在我的 viewDidLoad 中,我添加了这两个键盘通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name: UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];

这些是 2 个选择器方法:

-(void)keyboardWasShown:(NSNotification *)aNotification
{

if (displayKeyboard==YES) {
    return;
}
NSDictionary* info = [aNotification userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;

NSLog(@"kbw====%fkbh====%f",keyboardSize.width,keyboardSize.height);

offset = _scrollView.contentOffset;

CGRect viewFrame = _scrollView.frame;

NSLog(@"svw====%fsvh===%f",viewFrame.size.width,viewFrame.size.height);
viewFrame.size.height -= keyboardSize.height-49;
NSLog(@"new view hgt =====%f",viewFrame.size.height);
_scrollView.frame = viewFrame;

CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible: textFieldRect animated:YES];
displayKeyboard = YES;
}
-(void)keyboardWillBeHidden:(NSNotification *)aNotification
{

if (!displayKeyboard) {
    return; 
}

_scrollView.frame = CGRectMake(0, 0, 1024, 655);
_scrollView.contentOffset =offset;
displayKeyboard = NO;
}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}

displayKeyboard、offset 和 activeField 在 .h 文件中声明。

还要记得删除 viewDidDisappear:animated 中的通知

尽管此方法与前面所述的方法完全不同,但当 uikeyboard 处于活动状态时,在类之间导航时,此方法不会在顶部留下黑色部分。

我还注意到,如果我使用不推荐使用的UIKeyboardBoundsUserInfoKey方法来获得正确的键盘宽度和高度(我只在横向模式下工作)。而当我使用时UIKeyboardFrameBeginUserInfoKey,宽度和高度值被互换了。我仍在努力解决这个问题。

此外,当键盘出现时,它的上方附加了一个 49px 的固定空间。我假设那是我的标签栏高度,因此减去了 49。

于 2012-06-15T11:35:51.703 回答