Tek Yin
Tek Yin

Reputation: 3050

UIScrollView scrolled after setContentSize

So I want to create some detail content in UIView(320,470) that taller than Viewport (320, 367).

I create it separated in IB like (see pic.). Everything looks OK until I setContentSize to make the UIScrollView scrollable..

This is my code placed in ViewDidLoad

CGRect frame = self.uiContent.frame;
[self.uiScrollView addSubview:self.uiContent];
[self.uiScrollView setContentSize:frame.size];

The content is scrolled with animation to middle after setContentSize is called.. How to prevent that auto-scroll?

enter image description here

Upvotes: 0

Views: 1034

Answers (1)

Tek Yin
Tek Yin

Reputation: 3050

I found the culprit.. It was UITextView. Sorry if I don't mention I use UITextView for multiline label under address label.

Quoting "Taketo Sano" on other question : https://stackoverflow.com/a/5673026/453407

I've investigated how the auto-scroll is done by tracking the call-trace, and found that an internal [UIFieldEditor scrollSelectionToVisible] is called when a letter is typed into the UITextField. This method seems to act on the UIScrollView of the nearest ancestor of the UITextField.

UIScrollView is auto scrolled to UITextView when UITextView text is changed. So I found the solution by subclassing UIScrollview and override

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {

and return nothing to disable the auto scroll... If you plan to use it in future, just use a bool variable to enable / disable it by using

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated {
    if (!self.disableAutoScroll) {
        [super scrollRectToVisible:rect animated:animated];
    }
}

so you can disable the autoscroll before you change the UITextView by code and enable it after.

Upvotes: 1

Related Questions