2010-07-21 6 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];  
} 

답변

0

은 아마이 Text and Web Programming Guide for the iOS 문서에 표시된 키보드 알림을 사용하려면 네 방향에서 올바른 리프팅을 구현하는 방법은 없나요, 나열을 참조하십시오 2-1 2-2 2-3

당신이 할 수있는 알림을받을 때 키보드 크기를 가져옵니다.

1

다음은 현재 내 모든 앱에서이 방법을 사용하고있는 방법입니다.

여기에서 속임수는보기가 방향을 알고 있지만 키보드가 아니라는 것입니다. 이것은 Landscape에서 키보드의 너비가 실제로 높이인지 비자인지를 나타냅니다.

콘텐츠 세트를 변경하는 사과에게 권장되는 방법을 수정하고 가로 방향을 지원 얻으려면, 내가 사용하는 것이 좋습니다 것입니다 다음

:

// 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 변수의 너비와 높이 값을 '바꿔 넣기'합니다. 각 방향에서 올바른 값이 사용되고 있는지 확인하십시오.

관련 문제