2011-12-15 2 views
3

저는 iOS 프로그래밍을 처음 사용하고 키보드로 가려진 UITextField를 편집 할 때 UIScrollView를 이동하는 데 문제가 있습니다. 코드는 바로 Apple's documentation입니다.하지만 어떤 이유로 작동하지 않습니다.키보드가 활성 UITextField를 덮고있을 때 UIScrollView가 스크롤하지 않습니다. (사과 예제 사용)

디버깅을 통해 알림이 제대로 전달되는 것처럼 보입니다. 즉, "보기 크기를 조정해야합니다"라고 기록되지만 activeField가 키보드 아래에있는 textField 인 경우에만) 스크롤 점이 올바르게 설정되어 있지만 스크롤 뷰는 여전히 움직이지 않습니다. 또한, 나는 코드가 문서에서 직선으로보고 (의 ViewController가있는 ScrollView뿐만 아니라 텍스트 필드의 대표이다)

- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
scrollView.contentInset = contentInsets; 
scrollView.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 aRect = self.view.frame; 
aRect.size.height -= kbSize.height; 
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); 
    [scrollView setContentOffset:scrollPoint animated:YES]; 
    NSLog(@"%@",@"view should resize"); 
} 
} 

위임 패턴이 올바른지 합리적으로 확신, 아마 단순한 뭔가 빠진거야. 누구든지 확인할 물건 방향으로 나를 가리킬 수 있습니까?

+0

: 내보기 컨트롤러에이 코드를 추가하여이 문제를 해결 당신은 NSLog'에 마지막 줄을 변경할 수 있습니다 ("보기 크기를 조정해야한다"@)' – PengOne

+0

http://stackoverflow.com/a/672003/19679 –

+0

적절한 대리인을 정하셨습니까? – jakenberg

답변

1

Apple의 예제에는 스크롤 뷰의 콘텐츠 크기가 명시 적으로 설정되어 있지 않으므로 (0, 0) 기본 콘텐츠 크기가 사용된다는 버그가 있습니다. 참고로

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Set the scroll view's content size to be the same width as the 
    // application's frame but set its height to be the height of the 
    // application frame minus the height of the navigation bar's frame 
    CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame]; 
    CGRect navigationFrame = [[self.navigationController navigationBar] frame]; 
    CGFloat height = applicationFrame.size.height - navigationFrame.size.height; 
    CGSize newContentSize = CGSizeMake(applicationFrame.size.width, height); 

    ((UIScrollView *)self.view).contentSize = newContentSize; 
} 
관련 문제