I have a UITextView with a height of let's say 300. What I would like is when the typed 开发者_如何学编程text gets to the half way point, for the scrolling to start as if it were at the bottom of the textView. Basically I would like to programmatically set the point within the textView for scrolling to begin. Any ideas? Thanks.
Do you want to do this because the keyboard is causing the text to be hidden behind it? If so just size the textview smaller when the keyboard is raised (possibly animated so that it shrinks as the keyboard grows) then let the system take care of scrolling.
If you are trying to make the text scroll in a different way to the default maybe you should reconsider, it will look a bit strange to people who are used to it scrolling when it reaches the "normal" level.
However if you must do it maybe you could use a UIImageView added as a subview after the UITextView is raised - consider it as an inactive extension of the UITextView. Make an image of the same width as the UITextView, make it the right height for your requirements and position it so that it covers the lower edge of the UITextView. It will look like the textview is longer than it is but the textview's scrolling will work when text nears its actual border, not your fake one.
I can't see anything in UITextView/UIScrollView/UIScrollViewDelegate to do this.
Thanks, Adam. I am indeed doing this because the keyboard is causing text to be hidden behind and I'll get to that in a sec.
What you suggest with respect to the UIImageView is exactly what I was planning but in reverse. I wanted the 300 height textView, with the lower portion covered by the keyboard to then have a UIImageView sliver just above the keyboard of the rounded bottom portion of the grouped tableView that the textView is in. (The textView is in a tableViewCell in a single section/single cell tableView.) Adding the sliver of an image would make the larger textView, covered by the keyboard look like a small textView when the keyboard is present, and when dismissed and image removed/restored, textView would be larger, to allow the user to read all of the notes they've entered.
With the solution that I was looking for, scrolling to stop anywhere within the textView, the smaller textView would start scrolling at the correct place, just above the sliver image which is just above the keyboard.
Now to the "just size the textView" suggestion. Easier said than done. Because the textView is in a grouped tableView, with keyboard present, I created a smaller textView in a tableViewCell with nicely rounded corners, but when I size the textView larger, only the center portion extends (about 90% of the rounded rectangle in the center), not the rounded corners on the sides (5% of the width on each side remains the same). Next thought: Can't extend the textView without increasing the height of the cell so...
...the solution is therefore to resize the cell; but that involves a reloadData or begin/end updates which only work with a cell or section insert. I was then thinking of just adding a new section so the reloadData would work, but then I'd have to turn off scrolling on the scrollView so the new section would never be seen and I'm having trouble with that despite trying every permutation of scrollEnabled=NO (on tableView, textView, scrollView, parent).
And finally, shifting the entire screen upward works beautifully but with a large textView, I'd then have to prevent typing in the non-visible upper portion when scrolled upward, so that was rejected. Although it just occurred to me that with the CGRect's size.height equally reduced, typing may not occur in the non-visible portion - but I still lose the rounded top of the grouped tableView so cosmetically it could look better - still rejected.
So, that's my story and that's been my day. Any help from anyone would be greatly appreciated.
I think my best bet is adding the "fake" section to the tableView and setting it to hidden, so I can get the new cell height to kick in. but I still have to turn off tableView scrolling or scrollView scrolling so a scroll in the textView doesn't unnecessarily move the entire screen since the space for the hidden section would probably be there and therefore scrollable.
精彩评论