ios 每次更改文本时,如何强制 UITextView 滚动到顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1234242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force a UITextView to scroll to the top every time I change the text?
提问by Sam Stewart
OK, I'm having some problem with the UITextView
. Here's the issue:
好的,我的UITextView
. 这是问题:
I add some text to a UITextView
. The user then double clicks to select something.
I then change the text in the UITextView
(programatically as above) and the UITextView
scrolls to the bottom of the page where there is a cursor.
我将一些文本添加到UITextView
. 然后用户双击以选择某些内容。然后我更改UITextView
(以编程方式如上)中的文本,然后UITextView
滚动到页面底部有光标的位置。
However, that is NOT where the user clicked. It ALWAYS scrolls to the bottom of the UITextView
regardless of where the user clicked.
然而,这不是用户点击的地方。UITextView
无论用户点击哪里,它总是滚动到底部。
So here's my question: How do I force the UITextView
to scroll to the top every time I change the text? I've tried contentOffset
and scrollRangeToVisible
. Neither work.
所以这是我的问题:UITextView
每次更改文本时如何强制滚动到顶部?我试过contentOffset
和scrollRangeToVisible
。都不工作。
Any suggestions would be appreciated.
任何建议,将不胜感激。
回答by Wayne Lo
UITextView*note;
[note setContentOffset:CGPointZero animated:YES];
This does it for me.
这对我有用。
Swift version (Swift 4.1 with iOS 11 on Xcode 9.3):
Swift 版本(Xcode 9.3 上的 Swift 4.1 和 iOS 11):
note.setContentOffset(.zero, animated: true)
回答by nerd2know
If anyone has this problem in iOS 8, I found that just setting the UITextView's
text property, then calling scrollRangeToVisible
with an NSRange
with location:0
, length:0
, worked. My text view was not editable, and I tested both selectable and not selectable (neither setting affected the result). Here's a Swift example:
如果有人在 iOS 8 中遇到此问题,我发现只需设置UITextView's
text 属性,然后scrollRangeToVisible
使用NSRange
with location:0
,调用即可length:0
。我的文本视图不可编辑,我测试了可选择和不可选择(两种设置都不会影响结果)。这是一个 Swift 示例:
myTextView.text = "Text that is long enough to scroll"
myTextView.scrollRangeToVisible(NSRange(location:0, length:0))
回答by miguel
And here is my solution...
这是我的解决方案......
override func viewDidLoad() {
textView.scrollEnabled = false
textView.text = "your text"
}
override func viewDidAppear(animated: Bool) {
textView.scrollEnabled = true
}
回答by RyanTCB
Calling
打电话
scrollRangeToVisible(NSMakeRange(0, 0))
works but call it in
工作,但调用它
override func viewDidAppear(animated: Bool)
回答by Maria
I was using attributedString in HTML with text view not editable. Setting the content offset did not work for me either. This worked for me: disable scroll enabled, set the text and then enable the scrolling again
我在 HTML 中使用了 attributedString,文本视图不可编辑。设置内容偏移对我也不起作用。这对我有用:禁用滚动启用,设置文本,然后再次启用滚动
[yourTextView setScrollEnabled:NO];
yourTextView.text = yourtext;
[yourTextView setScrollEnabled:YES];
回答by Drew S.
Since none of these solutions worked for me and I wasted way too much time piecing together solutions, this finally solved it for me.
由于这些解决方案都不适合我,而且我浪费了太多时间拼凑解决方案,这终于为我解决了。
override func viewWillAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), {
let desiredOffset = CGPoint(x: 0, y: -self.textView.contentInset.top)
self.textView.setContentOffset(desiredOffset, animated: false)
})
}
This is really silly that this is not default behavior for this control.
这不是此控件的默认行为,这真的很愚蠢。
I hope this helps someone else out.
我希望这可以帮助其他人。
回答by Benjamin Sussman
I'm not sure if I understand your question, but are you trying to simply scroll the view to the top? If so you should do
我不确定我是否理解您的问题,但是您是否想简单地将视图滚动到顶部?如果是这样你应该做
[textview scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];
[textview scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];
回答by ChallengerGuy
For iOS 10 and Swift 3 I did:
对于 iOS 10 和 Swift 3,我做了:
override func viewDidLoad() {
super.viewDidLoad()
textview.isScrollEnabled = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textView.isScrollEnabled = true
}
Worked for me, not sure if you're having the problem with the latest version of Swift and iOS.
为我工作,不确定您是否在使用最新版本的 Swift 和 iOS 时遇到问题。
回答by MacInnis
I have an updated answer which will have the textview appear properly, and without the user experiencing a scroll animation.
我有一个更新的答案,它将使 textview 正确显示,并且没有用户体验滚动动画。
override func viewWillAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), {
self.introductionText.scrollRangeToVisible(NSMakeRange(0, 0))
})
回答by Maksym Horbenko
That's how I did it
我就是这样做的
Swift 4:
斯威夫特 4:
extension UITextView {
override open func draw(_ rect: CGRect)
{
super.draw(rect)
setContentOffset(CGPoint.zero, animated: false)
}
}