UITextView is not scrolled to top when loaded
UITextView is a subclass of UIScrollView, so you can use its methods. If all you want to do is ensure that it's scrolled to the top, then wherever the text is added try:
[self.mainTextView setContentOffset:CGPointZero animated:NO];
EDIT: AutoLayout with any kind of scrollview gets wonky fast. That setting a fixed width solves it isn't surprising. If it doesn't work in -viewDidLayoutSubviews
then that is odd. Setting a layout constraint manually may work. First create the constraints in IB:
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewWidthConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeightConstraint;
then in the ViewController
-(void)updateViewConstraints {
self.textViewWidthConstraint.constant = self.view.frame.size.width - 40.0f;
self.textViewHeightConstraint.constant = self.view.frame.size.height - 40.0f;
[super updateViewConstraints];
}
May still be necessary to setContentOffset in -viewDidLayoutSubviews
.
(Another method would be to create a layout constraint for "'equal' widths" and "'equal' heights" between the textView and its superView, with a constant of "-40". It's only 'equal' if the constant is zero, otherwise it adjusts by the constant. But because you can only add this constraint to a view that constraints both views, you can't do this in IB.)
You may ask yourself, if I have to do this, what's the point of AutoLayout? I've studied AutoLayout in depth, and that is an excellent question.
UITextview does not scroll to top
Use contentOffset
property:
textView.contentOffset = CGPointZero
Update for Swift 3:
textView.contentOffset = CGPoint.zero
Scroll TextView to the top
I had a very similar issue, especially when using splitview and testing on the iPhoneX, I resolved this by incorporating this bit of code in my ViewController when I needed the textView to scroll to the top:
textView.setContentOffset(.zero, animated: false)
textView.layoutIfNeeded()
If you wish to scroll to the top of the textView upon loading your ViewController:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Can add an if statement HERE to limit when you wish to scroll to top
textView.setContentOffset(.zero, animated: false)
}
How to scroll to top of UITextView?
The most obvious solution is to set the location
parameter of NSMakeRange
to 0 instead of theTextView.text.characters.count - 1
.
let bottom = NSRange(location: 0, length: 1)
A better way is to note that UITextView
extends UIScrollView
. So you can set the contentOffset
:
theTextView.contentOffset = .zero
If your want to animate the scrolling, use:
theTextView.setContentOffset(.zero, animated: true)
Related Topics
Abpeoplepickernavigationcontroller Changes with iOS8
How to Add Images to the iOS Simulator
How to Install iOS 7.0 and iOS 8.0 Simulators in Xcode 6.1
When Does Awakefromnib Get Called
In iOS, What's the Difference Between Autoresizing, Autolayout, and Constraints
Xcode - How to Drag a Component from One View to Another Without Losing Its Frame
iOS Launch Images - Driving Me Crazy
Intercept Objective-C Delegate Messages Within a Subclass
How to Hide the Keyboard When I Press Return Key in a Uitextfield
How to Increase Line Spacing in Uilabel in Swift
How to Obtain Certificate Signing Request
Switching Between Text Fields on Pressing Return Key in Swift
How to Add Particle Effects to an iOS App That Is Not a Game Using iOS 7 Spritekit Particle
How to Crop Image Inside the Circle in Uiimageview in iOS
Watchkit Extension Bundle Identifiers
How to Respond to Push Notification View If App Is Already Running in the Background