滚动完成后如何隐藏iPhone键盘

时间:2009-12-01 11:34:34

标签: iphone objective-c

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification  object:self.view.window];
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [super viewWillDisappear:animated];
}

-(void)keyboardWillShow:(NSNotification *)notif
{
    NSDictionary*info=[notif userInfo];
    NSValue* aValue= [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize=[aValue CGRectValue].size;
    float bottomPoint= (text1.frame.origin.y+text1.frame.size.height+5);
    //float bottomPoint= (text2.frame.origin.y+text2.frame.size.height+5);
    //float bottomPoint= (text3.frame.origin.y+text3.frame.size.height+5);
    scrollAmount=keyboardSize.height - (self.view.frame.size.height - bottomPoint);
    if(scrollAmount > 0)
    {
        moveViewUp = YES;
        [self scrollTheView:YES];
    }
    else
        moveViewUp = NO;
    }
}

-(void)scrollTheView:(BOOL) movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    CGRect rect = self.view.frame;
    if(movedUp)
    { 
        rect.origin.y -= scrollAmount;
    }
    else 
    {
        rect.origin.y += scrollAmount;
    }
    self.view.frame = rect;
    [UIView commitAnimations];
}

-(BOOL)text1ShouldReturn:(UITextField  *)theTextField
{
    [theTextField resignFirstResponder];
    if(moveViewUp)
        [self scrollTheView:NO];
    return YES;
}

我已经编写了这段代码来滚动视图,但是当键盘消失时它没有返回。

1 个答案:

答案 0 :(得分:1)

您还没有订阅UIKeyboardWillHideNotifcation,只有Show - 因此当键盘隐藏时它不会执行任何操作。