2013-02-09 4 views
7

UITextView는 모달 컨트롤러보기의 하위보기입니다. UITextView의 아래쪽 경계 y 좌표가 키보드의 위쪽 y 좌표와 같도록 키보드가 나타날 때 UITextView 높이를 줄여야합니다. I'getting 키보드 높이iPad의 UIModalPresentationFormSheet. 키보드가 나타날 때 UITextView 높이를 조절하는 방법

CGRect frameBegin = [[notification.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] ; 
CGRect frameEnd = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

CGRect resultBegin = [self.view convertRect:frameBegin fromView:nil]; 
CGRect resultEnd = [self.view convertRect:frameEnd fromView:nil]; 

CGFloat kbdHeight = resultBegin.origin.y - resultEnd.origin.y; 

문제는 키보드가 나타날 때이 모달 뷰가 이동한다는 것입니다. 이 경우 키보드의 상단 테두리 좌표를 계산하는 방법은 무엇입니까?

답변

4

이 작업을 수행 할 수 있습니다 :

1. Register for keyboard notifications: 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(myTextViewHeightAdjustMethod:) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(myTextViewHeightAdjustMethod:) 
              name:UIKeyboardDidShowNotification 
              object:nil]; 

2. Calculate intersection and adjust textView height with the bottom constraint 

    - (void)myTextViewHeightAdjustMethod:(NSNotification *)notification 
    { 
     NSDictionary *userInfo = [notification userInfo]; 
     CGRect keyboardFinalFrame = [[userInfo emf_ObjectOrNilForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 

     CGPoint keyboardOriginInView = [self.view convertPoint:keyboardFinalFrame.origin fromView:nil]; 

      CGFloat intersectionY = CGRectGetMaxY(self.view.frame) - keyboardOriginInView.y; 

      if (intersectionY >= 0) 
      { 
       self.textViewBottomConstraint.constant = intersectionY + originalTextViewBottomConstraint; 

       [self.textView setNeedsLayout]; 
    } 

은 알림의 등록을 취소해야합니다.

+0

질문을 한 이후로 많은 일이있었습니다. 어쨌든 대답에 감사드립니다. – Michael

0

당신은 내가 사용하는 것이 좋습니다이 자신을 코드를 작성할 필요가없는 경우 https://github.com/hackiftekhar/IQKeyboardManager

그것은 (지금까지 나를 위해) 좋은 작품과 당신이 할 필요가 그것을 가져 와서이 한 줄을 추가입니다 AppDelegate의 코드 :

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    //Magic one-liner 
    IQKeyboardManager.sharedManager().enable = true 

    return true 
} 
관련 문제