2012-06-13 16 views
0

我在ipad上有一個窗體,它有很多文本框和一個按鈕。當鍵盤處於活動狀態時,會出現一些字段。爲了將隱藏在鍵盤後面的texfield拖到可見狀態,我使用下面的代碼。uikeyboard在導航上留下黑色部分

- (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]; 
} 
} 

我也在使用滾動視圖。我的問題是,當鍵盤處於活動狀態時,按下我的按鈕,它會將我帶到下一個屏幕。現在,如果我導航回來,鍵盤是活動的,並設置了先前的動畫。現在,如果我隱藏鍵盤,則整個視圖向下滾動,在上面留下黑色部分。如何處理這種情況?

回答

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,偏移和activeField在.h文件中聲明。

還記得刪除viewDidDisappear通知:動畫

雖然這種方法是完全不同的比之前的規定,這一個不頂部留下一個黑色的部分,而類之間導航時的uikeyboard活躍。

另外我注意到的是,如果我使用不合時宜的UIKeyboardBoundsUserInfoKey我用來獲得正確的鍵盤寬度和高度(我只在橫向模式下工作)。而當我使用UIKeyboardFrameBeginUserInfoKey寬度和高度值互換。我仍然試圖找出這個問題。

此外當鍵盤用於出現時,49px的固定空間被追加到它上面。我認爲這是我的標籤欄高度,因此減去49.