UITextView is not scrolled to top when loaded

后端 未结 12 1232
别跟我提以往
别跟我提以往 2020-12-03 02:39

When I have text that does not fill the UITextView, it is scrolled to the top working as intended. When there is more text than will fit on screen, the UITextView is scrolle

相关标签:
12条回答
  • 2020-12-03 03:04

    If you don't wanna mess with constraints:

    override func updateViewConstraints() {
        super.updateViewConstraints()
    }
    
    override func viewDidLayoutSubviews() {
        self.textLabel.setContentOffset(CGPointZero, animated: false)
    }
    
    0 讨论(0)
  • 2020-12-03 03:05

    Swift

    override func viewDidLoad() {
        super.viewDidLoad()  
        textView.isScrollEnabled = false  
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        textView.isScrollEnabled = true
    }
    
    0 讨论(0)
  • 2020-12-03 03:10

    David Rectors answer in Objective C:

    #import "TopTextView.h"
    
    @implementation TopTextView
    
    bool scrolled = NO;
    
    - (void) layoutSubviews
    {
        [super layoutSubviews];
        if (!scrolled) {
            [self setContentOffset:CGPointMake(0, 0) animated:NO];
            scrolled = YES;
        }
    }
    
    @end
    
    0 讨论(0)
  • 2020-12-03 03:11

    This is an interesting bug. In our project, this is only occurring on devices with an iPhone 5-size screen. It appears that the textview contentOffset changes at some point during the view controller lifecycle. In viewDidLoad and viewWillAppear the textview's contentOffset is 0,0, and by viewDidAppear it's changed. You can see it happening in viewWillLayoutSubviews. Constraints appear to be set up correctly.

    This will ensure you don't call a scrolling method unless it's needed:

    if textView.contentOffset.y > 0 {
        textView.contentOffset = CGPoint(x: 0, y: 0)
        // Or use scrollRectToVisible, scrollRangeToVisible, etc.
    }
    
    0 讨论(0)
  • 2020-12-03 03:13

    add the following function to your view controller class...

    Swift 3

    override func viewDidLayoutSubviews() {
        self.mainTextView.setContentOffset(.zero, animated: false)
    }
    

    Swift 2.1

    override func viewDidLayoutSubviews() {
        self.mainTextView.setContentOffset(CGPointZero, animated: false)
    }
    

    Objective C

    - (void)viewDidLayoutSubviews {
        [self.mainTextView setContentOffset:CGPointZero animated:NO];
    }
    
    0 讨论(0)
  • 2020-12-03 03:13

    For me this works in a different way, I tried all things mentioned above but none of the worked in func viewWillAppear(_ animated: Bool). Which eventually makes textView scrolled up, and in func viewDidAppear(_ animated: Bool) it would scroll after screen appeared.

    Below worked for me but got some constraint related issue with keyboard up and down.

    override func viewDidLayoutSubviews() {
        self.textView.setContentOffset(.zero, animated: false)
    }
    

    Below worked as expectation:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.textView.scrollsToTop = true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.layoutIfNeeded()
        self.textView.setContentOffset(.zero, animated: false)
    }
    
    0 讨论(0)
提交回复
热议问题