UIScrollView ScrollRectToVisible - does not work with animation = yes

I have a UIScrollView that contains a button. When the button is pressed, I would like to scroll it to the bottom of the screen using scrollRectToVisible.

eg:

CGRect r = CGRectMake(0, myUIScrollView.contentSize.height - 1, 1, 1);
[myUIScrollView scrollRectToVisible:r animated:YES];

If I set the animation to NO, everything will work as expected, but if I set it to YES, I see a really strange behavior:

  • in principle, nothing happens.
  • If I press a button several times, it can scroll a couple of pixels, or it can scroll completely.
  • but if I scroll the view manually with my finger before pressing the button, it has a chance to scroll to the bottom, as expected, but this is not necessary.

I printed _geScroll_Settings.contentSize and it is expected.

scrollRectToVisible, , .

ScrollView . . scrollView contentSize, , , .

?

+5
2

, scrollRectToVisible , (1x1), y , scrollView?

CGRect rectBottom = CGRectZero;
rectBottom.size = myUIScrollView.frame.size;
rectBottom.origin.y = myUIScrollView.contentSize.height - rectBottom.size.height;
rectBottom.origin.x = 0;

[myUIScrollView scrollRectToVisible:rectBottom animated:YES];

, , Mac, . CGRect , scrollView, .

+5

, " NO, ".

, iOS 6 UITextView UIScrollView, , . iOS 7 . UIScrollView, , RectToVisible .

iOS 6 scrollRectToVisible . , UITextView ( ), , iOS 7.

, Xcode 5, 6.0 ViewController.m. iOS 6.1, , UITextView . , , . WORKAROUD, UITextView UIScrollView, scrollRectToVisible , .

#import "ViewController.h"

//#define WORKAROUND

@interface ViewController ()
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITextView *textView;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap)]];

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
    self.scrollView.contentSize = CGSizeMake(320, 400);
    self.scrollView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.scrollView];

#ifdef WORKAROUND
    UIScrollView* dummyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
    self.textView = [[UITextView alloc] initWithFrame:dummyScrollView.bounds];
    [dummyScrollView addSubview:self.textView];
    [self.scrollView addSubview:dummyScrollView];
#else
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 280, 280, 100)];
    [self.scrollView addSubview:self.textView];
#endif

    self.textView.backgroundColor = [UIColor grayColor];

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

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewTap
{
    if (self.textView.isFirstResponder) {
        [self.textView resignFirstResponder];
    }
    else {
        [self.textView becomeFirstResponder];
    }
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
#ifdef WORKAROUND
    [self.scrollView scrollRectToVisible:CGRectInset(self.textView.superview.frame, 0, -10) animated:YES];
#else
    [self.scrollView scrollRectToVisible:CGRectInset(self.textView.frame, 0, -10) animated:YES];
#endif
}

@end
0

All Articles