开发者

UIScrollView scrolling to bottom when UITextField text changes

As it is set up now, I have two UITextViews inside of a UIScrollView. The point of this is that the UITextViews themselves don't scroll, they just get larger (their contentsize) as more text is added. The UIScrollView handles all of the scrolling up and down of the view regardless of how much text there is (think similar to Mail.app where the subject view is above the message view, etc). That said, I've ran into a problem now. When I programmatically add text to my UITextView (in this code, bodyText), the UIScrollView automatically scrolls to the bottom for some reason.

To add the text I just do:

NSRange selectedRange = [bodyText selectedRange];
NSString *selectedText = [bodyText.text substringWithRange:selectedRange];
bodyText.text = [bodyText.text stringByReplacingCharactersInRange:selectedRange withString:[NSString stringWithFormat:@"<b>%@</b>", selectedText]];

So, for instance, if I had 2000 pixels vertically of text and was currently scrolled to position 400 and then added text somewhere around there, the UIScrollView would then go all the way down to 2000.

I've tried开发者_开发百科 to stop it via subclassing UISCrollView and overriding -setContentOffset, but that freezes all scrolling then.

My question is, why does it scroll all the way to the bottom in the first place? The text is added when the user clicks a button somewhere else on the screen, and the scrolling has nothing to do with any finger drags or anything.


In Scroll View Programming Guide you get:

Making a rectangle visible

It is also possible to scroll a rectangular area so that it is visible. This is especially useful when an application needs to display a control that is currently outside the visible area into the visible view. The scrollRectToVisible:animated: method scrolls the specified rectangle so that it is just visible inside the scroll view. If the animated parameter is YES, the rectangle is scrolled into view at a constant pace. As with setContentOffset:animated:, if animation is disabled, the delegate is sent a single scrollViewDidScroll: message. If animation is enabled, the delegate is sent a series of scrollViewDidScroll: messages as animation progresses. In the case of scrollRectToVisible:animated: the scroll view’s tracking and dragging properties are also NO.

If animation is enabled for scrollRectToVisible:animated:, the delegate receives a scrollViewDidEndScrollingAnimation: message, providing notification that the scroll view has arrived at the specified location and animation is complete.

So maybe you can use this functionality to lock the visibility on your UITextView.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜