2014-06-13 7 views
0

좋아요, 그래서 저는이 하나에 난처합니다. 키보드 통보 및 UIScrollView를 사용하여 도청 될 때 현재 활성 텍스트 필드로 스크롤합니다. 인물과 경관 모두에서 예상대로 정확히 작동합니다. 장치가 평평하게 놓여있는 경우를 제외하고는입니다.장치가 평평하게 놓여있는 동안 UIScrollView를 활성 UITextField로 이동하는 방법?

가로 방향에서는 장치를 공중에 놓으면 예상대로 스크롤되지만 장치를 테이블 위에 놓고 텍스트 필드를 두드리면보기가 화면에서 완전히 스크롤됩니다.

이 원인은 무엇입니까?

iOS7을 사용하는 iPad Mini 및 iPhone 5에서 테스트 중입니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 

    // Register for keyboard notifications 
    [self registerForKeyboardNotifications]; 

    // keep track of scrollview insets for later 
    scollViewIndicatorInsets = self.scrollView.scrollIndicatorInsets; 
} 

- (IBAction)editingEnded:(id)sender { 
    [sender resignFirstResponder]; 
} 

- (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 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) { 
     kbSize = CGSizeMake(kbSize.height, kbSize.width); 
    } 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(65.0, 0.0, kbSize.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your app might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
     [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; 
    } 
} 



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

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
} 

답변

1

키보드 크기를 계산하는 것이 훨씬 더 복잡합니다. API가 당신을 위해 일하게하십시오 :

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGRect kbFrame = [info[UIKeyboardFrameBeginUserInfoKey] CGRectValue]; 
    // Convert the keyboard frame from the window to the scrollview 
    kbFrame = [self.scrollView convertRect:kbFrame fromView:nil]; 
    CGSize kbSize = kbFrame.size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(65.0, 0.0, kbSize.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your app might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
     [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; 
    } 
} 
+0

그 트릭을! 더 간단합니다. 감사합니다. –