UITextView starts at Bottom or Middle of the text

后端 未结 19 1554
谎友^
谎友^ 2020-12-04 13:09

I\'ll get right to it. I have a UItextView placed in my view that when needs to scroll to see all the text (when a lot of text is present in the textView) the t

相关标签:
19条回答
  • 2020-12-04 13:15

    With a lot of testing, i found must add below in viewWillLayoutSubviews() function to make sure the UITextView show up from the very beginning:

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()            
        textViewTerms.scrollRangeToVisible(NSMakeRange(0, 0))
    }
    
    0 讨论(0)
  • 2020-12-04 13:15

    The following code should give you effect you want.

    [self.scrollView setContentOffset:CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];
    

    You'll need to replace "self.scrollView" with the name of your scroll view. You should put this code in after you've set the text of the scroll view.

    0 讨论(0)
  • 2020-12-04 13:16

    All of the answers above did not work for me. However, the secret turns out to be to implement your solution within an override of viewDidLayoutSubviews, as in:

    override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
    
      welcomeText.contentOffset = .zero
    }
    

    HTH :)

    0 讨论(0)
  • 2020-12-04 13:16

    In Swift 2

    You can use this to make the textView start from the top:

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    
        myTextView.setContentOffset(CGPointZero, animated: false)
    }
    

    Confirmed working in Xcode 7.2 with Swift 2

    0 讨论(0)
  • 2020-12-04 13:17

    This worked for me:

     override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        textView.scrollRectToVisible(CGRect(origin: CGPointZero, size: CGSizeMake(1.0, 1.0)), animated: false)
    
    }
    
    0 讨论(0)
  • 2020-12-04 13:18

    I had to implement two answers here to get my view working as I want:

    From Juan David Cruz Serrano:

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

    And from Murat Yasar:

    automaticallyAdjustsScrollViewInsets = false
    

    This gave a UITextView that loads with the scroll at the very top and where the insets are not changed once scrolling starts. Very strange that this is not the default behaviour.

    0 讨论(0)
提交回复
热议问题