2009-12-01 4 views
0
-(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; 
} 

보기를 스크롤하려면이 코드를 작성했지만 키보드가 사라지면 리턴하지 않습니다.스크롤 할 때 iPhone 키보드를 숨기는 방법

답변

1

UIKeyboardWillHideNotifcation을 구독하지 않았으므로 표시 만하므로 키보드가 숨겨져있을 때 아무 것도하지 않습니다.

관련 문제