2010-07-21 93 views
0

在我的iPhone應用程序中,我使用以下代碼來提升視圖以顯示鍵盤上方的文本字段。我的應用程序支持所有四種接口方向。我使用下面的代碼來解除視圖。這裏的問題是,它只適用於縱向。有什麼辦法來實現在所有四個方向如何在鍵盤上顯示所有界面方向的文本字段

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    UIInterfaceOrientation orientation = [Globals getUIInterfaceOrientation];//[[UIApplication sharedApplication] statusBarOrientation]; 
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 

    if (heightFraction < 0.0) { 
     heightFraction = 0.0; 
    } 
    else if (heightFraction > 1.0) { 
     heightFraction = 1.0; 
    } 
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) 
     animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    else 
     animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y -= animatedDistance; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame];  
    [UIView commitAnimations]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField{ 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y += animatedDistance; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 
    [self.view setFrame:viewFrame];  
    [UIView commitAnimations];  
} 

回答

1

以下是我目前如何在所有應用程序上實現此目的。

這裏的訣竅是雖然視圖知道方向,但鍵盤不是。這意味着在景觀中,鍵盤的寬度實際上是其高度,反之亦然。

要修改蘋果推薦的方式改變內容的插圖,並得到其支持橫向,我會建議使用下列內容:

// Call this method somewhere in your view controller setup code. 
- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(keyboardWasShown:) 
      name:UIKeyboardDidShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(keyboardWillBeHidden:) 
      name:UIKeyboardWillHideNotification object:nil]; 
} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { 
     CGSize origKeySize = keyboardSize; 
     keyboardSize.height = origKeySize.width; 
     keyboardSize.width = origKeySize.height; 
    } 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0); 
    scroller.contentInset = contentInsets; 
    scroller.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your application might not need or want this behavior. 
    CGRect rect = scroller.frame; 
    rect.size.height -= keyboardSize.height; 
    NSLog(@"Rect Size Height: %f", rect.size.height); 

    if (!CGRectContainsPoint(rect, activeField.frame.origin)) { 
     CGPoint point = CGPointMake(0, activeField.frame.origin.y - keyboardSize.height); 
     NSLog(@"Point Height: %f", point.y); 
     [scroller setContentOffset:point animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent  
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 

要注意這裏的部分如下:

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) { 
    CGSize origKeySize = keyboardSize; 
    keyboardSize.height = origKeySize.width; 
    keyboardSize.width = origKeySize.height; 
} 

什麼是,會檢測設備的方向。如果是橫向,它會將keyboardSize變量的寬度和高度值「交換」到確保每個方向都使用正確的值。

相關問題