2

У меня есть UITextView внутри UIScrollView, который должен автоматически прокручиваться после того, как пользователь начинает его редактировать. Это связано с тем, что клавиатура будет покрывать текст.UIScrollView программно Прокрутка

Вот код -

В viewDidLoad:

feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)]; 
feedBackformView.backgroundColor = [UIColor whiteColor]; 
feedBackformView.scrollEnabled = YES; 
feedBackformView.delegate = self; 
feedBackformView.userInteractionEnabled = YES; 
feedBackformView.showsVerticalScrollIndicator = YES; 
feedBackformView.contentSize = CGSizeMake(320, 700); 

commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)]; 
commentsView.delegate = self; 
commentsView.layer.borderWidth = 2.0f; 
commentsView.layer.cornerRadius = 5; 

и вот реализация метода делегата -

-(void)textViewDidBeginEditing:(UITextView *)textView{ 
    CGPoint point = textView.frame.origin; 
    [scrollView setContentOffset:point animated:YES]; 
} 

Однако, ничего не происходит.

+0

Что происходит при прокрутке с помощью scrollRectToVisible. – guenis

ответ

4

ваши делают хорошо, но использовать feedBackformView вместо Scrollview, при установке контента смещения в textViewDidBeginEditing: методе, просто посмотрите на приведенный ниже код

feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)]; 
feedBackformView.backgroundColor = [UIColor whiteColor]; 
feedBackformView.scrollEnabled = YES; 
feedBackformView.delegate = self; 
feedBackformView.userInteractionEnabled = YES; 
feedBackformView.showsVerticalScrollIndicator = YES; 
feedBackformView.contentSize = CGSizeMake(320, 700); 

commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)]; 
commentsView.delegate = self; 
commentsView.layer.borderWidth = 2.0f; 
commentsView.layer.cornerRadius = 5; 

[feedBackformView addSubview:commentsView]; 
[self.view addSubview:feedBackformView]; 

в TextView DelegateMethod,

-(void)textViewDidBeginEditing:(UITextView *)textView{ 
    CGPoint point = textView.frame.origin; 
    [feedBackformView setContentOffset:point animated:YES]; 
} 

надеюсь, что это будет hepls вас ...

+0

, который работал как шарм. благодаря –

1

Вы можете использовать Scrollview scrollRectToVisible:animated или setContentOffset:animated способы, описанные здесь: UIScrollView Class Reference

Имейте в виду, что UITextView приходит со своим собственным видом прокрутки. поэтому вам может не понадобиться вставлять его в другое окно прокрутки. Но вам может потребоваться, чтобы ваше приложение было интересным.

1

Ваша точка прокрутки уже в видимом месте?

-(void)textViewDidBeginEditing:(UITextView *)textView{ 
    // try this instead 
    CGPoint point = CGPointMake(textView.frame.origin.x, 
           textView.frame.origin.y + textView.frame.size.height); 
    [scrollView setContentOffset:point animated:YES]; 

    // What does this produce? 
    NSLog(@"%@ %@", NSStringFromCGPoint(scrollView.contentOffset), 
        NSStringFromCGRect(textView.frame)); 
} 
Смежные вопросы