Answers:
UITextView*note;
[note setContentOffset:CGPointZero animated:YES];
这为我做到了。
Swift版本(Xcode 9.3上带有iOS 11的Swift 4.1):
note.setContentOffset(.zero, animated: true)
viewDidAppear()
。
如果任何人有在iOS 8的这个问题,我发现只需设置UITextView's
文本属性,然后调用scrollRangeToVisible
与NSRange
用location:0
,length:0
,合作。我的文本视图不可编辑,并且我测试了可选择和不可选择(这两个设置都不会影响结果)。这是一个Swift示例:
myTextView.text = "Text that is long enough to scroll"
myTextView.scrollRangeToVisible(NSRange(location:0, length:0))
这是我的解决方案...
override func viewDidLoad() {
textView.scrollEnabled = false
textView.text = "your text"
}
override func viewDidAppear(animated: Bool) {
textView.scrollEnabled = true
}
呼唤
scrollRangeToVisible(NSMakeRange(0, 0))
可以,但是打电话
override func viewDidAppear(animated: Bool)
我在HTML中使用attributedString,且文本视图不可编辑。设置内容偏移量对我也不起作用。这对我有用:禁用滚动启用,设置文本,然后再次启用滚动
[yourTextView setScrollEnabled:NO];
yourTextView.text = yourtext;
[yourTextView setScrollEnabled:YES];
viewDidAppear
作为更广泛的严重补丁的一部分
由于这些解决方案都不适合我,并且我浪费了太多时间拼凑解决方案,因此最终为我解决了问题。
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)
})
}
这不是此控件的默认行为,这真是愚蠢。
我希望这可以帮助其他人。
self.textView.contentOffset.y
基于此答案使用了另一个偏移量计算:stackoverflow.com/a/25366126/1736679
我不确定我是否理解您的问题,但是您是否只是想将视图滚动到顶部?如果是这样,您应该这样做
[textview scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];
对于iOS 10和Swift 3,我做到了:
override func viewDidLoad() {
super.viewDidLoad()
textview.isScrollEnabled = false
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textView.isScrollEnabled = true
}
为我工作,不确定您是否遇到最新版本的Swift和iOS问题。
我有一个更新的答案,它将使textview正确显示,并且用户没有滚动动画。
override func viewWillAppear(animated: Bool) {
dispatch_async(dispatch_get_main_queue(), {
self.introductionText.scrollRangeToVisible(NSMakeRange(0, 0))
})
这是它在iOS 9 Release上的工作方式,因此textView在出现在屏幕上之前先在顶部滚动
- (void)viewDidLoad
{
[super viewDidLoad];
textView.scrollEnabled = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
textView.scrollEnabled = YES;
}
我就是那样做的
斯威夫特4:
extension UITextView {
override open func draw(_ rect: CGRect)
{
super.draw(rect)
setContentOffset(CGPoint.zero, animated: false)
}
}
这对我有用
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
DispatchQueue.main.async {
self.textView.scrollRangeToVisible(NSRange(location: 0, length: 0))
}
}
DispatchQueue.main.async(...
不需要打入电话,但由于某些原因,在(例如)iPhone 5s(iOS 12.1)上,除非您调度,否则它将无法正常工作。但是,正如调试器上的调用堆栈所报告的,在所有情况下viewWillAppear()
,该主队列都已在运行 ...-WTF,Apple?
尝试将光标移动到文本顶部。
NSRange r = {0,0};
[yourTextView setSelectedRange:r];
看看情况如何。确保在触发所有事件或完成所有操作后调用此方法。
我的问题是将view设置为将textView滚动到顶部。对于iOS 10.3.2和Swift 3。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// It doesn't work. Very strange.
self.textView.setContentOffset(CGPoint.zero, animated: false)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// It works, but with an animation effection.
self.textView.setContentOffset(CGPoint.zero, animated: false)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// It works.
self.textView.contentOffset = CGPoint.zero
}
结合之前的答案,现在应该可以了:
talePageText.scrollEnabled = false
talePageText.textColor = UIColor.blackColor()
talePageText.font = UIFont(name: "Bradley Hand", size: 24.0)
talePageText.contentOffset = CGPointZero
talePageText.scrollRangeToVisible(NSRange(location:0, length:0))
talePageText.scrollEnabled = true
scrollRangeToVisible
。
在Swift 3中
viewWillLayoutSubviews是进行更改的地方。viewWillAppear也应该工作,但是从逻辑上讲,布局应该在viewWillLayoutSubviews中执行。
关于方法,scrollRangeToVisible和setContentOffset都可以使用。但是,setContentOffset允许关闭或打开动画。
假设UITextView被命名为yourUITextView。这是代码:
// Connects to the TextView in the interface builder.
@IBOutlet weak var yourUITextView: UITextView!
/// Overrides the viewWillLayoutSubviews of the super class.
override func viewWillLayoutSubviews() {
// Ensures the super class is happy.
super.viewWillLayoutSubviews()
// Option 1:
// Scrolls to a range of text, (0,0) in this very case, animated.
yourUITextView.scrollRangeToVisible(NSRange(location: 0, length: 0))
// Option 2:
// Sets the offset directly, optional animation.
yourUITextView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}
这对我有用。它基于RyanTCB的答案,但是是同一解决方案的Objective-C变体:
- (void) viewWillAppear:(BOOL)animated {
// For some reason the text view, which is a scroll view, did scroll to the end of the text which seems to hide the imprint etc at the beginning of the text.
// On some devices it is not obvious that there is more text when the user scrolls up.
// Therefore we need to scroll textView to the top.
[self.textView scrollRangeToVisible:NSMakeRange(0, 0)];
}
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
[textview setContentOffset:CGPointZero animated:NO];
}
-(void) viewDidAppear:(BOOL)animated{
[mytextView scrollRangeToVisible:NSMakeRange(0,0)];}
- (void)viewWillAppear:(BOOL)animated{
[mytextView scrollRangeToVisible:NSMakeRange(0,0)];}
ViewWillappear
会在用户注意到之前立即执行此操作,但是ViewDidDisappear
section将对其进行动画处理。[mytextView setContentOffset:CGPointZero animated:YES];
有时不起作用(我不知道为什么),所以请使用scrollrangetovisible。我遇到了问题,但在我的情况下,textview是某些自定义视图的子视图,并将其添加到ViewController。没有一种解决方案对我有用。为了解决该问题,添加了0.1秒的延迟,然后启用了滚动。它为我工作。
textView.isScrollEnabled = false
..
textView.text = // set your text here
let dispatchTime = DispatchTime.now() + 0.1
DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
self.textView.isScrollEnabled = true
}
对我来说,这段代码不错:
textView.attributedText = newText //or textView.text = ...
//this part of code scrolls to top
textView.contentOffset.y = -64
textView.scrollEnabled = false
textView.layoutIfNeeded() //if don't work, try to delete this line
textView.scrollEnabled = true
为了滚动到确切位置并将其显示在屏幕顶部,我使用以下代码:
var scrollToLocation = 50 //<needed position>
textView.contentOffset.y = textView.contentSize.height
textView.scrollRangeToVisible(NSRange.init(location: scrollToLocation, length: 1))
设置contentOffset.y会滚动到文本的末尾,然后scrollRangeToVisible会向上滚动到scrollToLocation的值。因此,所需位置出现在scrollView的第一行中。
对于我来说,在iOS 9中,它停止了使用scrollRangeToVisible方法的属性字符串为我工作(第1行使用粗体,其他行使用常规字体)
所以我不得不使用:
textViewMain.attributedText = attributedString
textViewMain.scrollRangeToVisible(NSRange(location:0, length:0))
delay(0.0, closure: {
self.textViewMain.scrollRectToVisible(CGRectMake(0, 0, 10, 10), animated: false)
})
延迟在哪里:
func delay(delay:Double, closure:()->Void) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
尝试使用以下两行解决方案:
view.layoutIfNeeded()
textView.contentOffset = CGPointMake(textView.contentInset.left, textView.contentInset.top)
这是我的代码。它工作正常。
class MyViewController: ...
{
private offsetY: CGFloat = 0
@IBOutlet weak var textView: UITextView!
...
override viewWillAppear(...)
{
...
offsetY = self.textView.contentOffset.y
...
}
...
func refreshView() {
let offSetY = textView.contentOffset.y
self.textView.setContentOffset(CGPoint(x:0, y:0), animated: true)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
[unowned self] in
self.textView.setContentOffset(CGPoint(x:0, y:self.offSetY),
animated: true)
self.textView.setNeedsDisplay()
}
}