Uiscrollview Scroll to Bottom Programmatically

UIScrollView scroll to bottom programmatically

You can use the UIScrollView's setContentOffset:animated: function to scroll to any part of the content view. Here's some code that would scroll to the bottom, assuming your scrollView is self.scrollView:

Objective-C:

CGPoint bottomOffset = CGPointMake(0, self.scrollView.contentSize.height - self.scrollView.bounds.size.height + self.scrollView.contentInset.bottom);
[self.scrollView setContentOffset:bottomOffset animated:YES];

Swift:

let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.height + scrollView.contentInset.bottom)
scrollView.setContentOffset(bottomOffset, animated: true)

Hope that helps!

How to set UIScrollView scrolled to bottom?

You can try below code for moving scroll view to the bottom of the screen:

extension UIScrollView {

func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
setContentOffset(bottomOffset, animated: true)
}
}

Add this extension and try below code to access this extension for scroll to the bottom:

    scrlView.scrollToBottom()

If it is not working properly then you can also try to execute in mail queue:

DispatchQueue.main.async {
scrlView.scrollToBottom()
}

Let me know if you face any problem.

scrolling a scrollview to its bottom programmatically

The offset setting doesn't works because you tried in calling early in the life cycle.

You could try updating the contentOffset at viewDidLayoutSubviews or viewDidAppear

let bottomOffset = CGPoint(x: 0, y: scrollView.contentSize.height - scrollView.bounds.size.height)
scrollView.setContentOffset(bottomOffset, animated: true)

scrolling the scrollview at the bottom programmatically - iphone

Use something like this instead:

CGPoint bottomOffset = CGPointMake(0, [yourScrollView contentSize].height);
[yourScrollView setContentOffset:bottomOffset animated:YES];

If you don't want it animated, just change YES to NO.

Programmatically scroll uiscrollview to bottom as uilabel grows in height

Again, the first thing you need to do is layout your subviews after updating the text so your computations are based on the new text.

Rather than jumping through a lot of hoops and trying to compute the contentOffset I would suggest using scrollToRectVisible(_:animated:).

    view.layoutIfNeeded()

let rect = CGRect(
x: 0,
y: transcriptionLabel.frame.maxY - 1,
width: 1,
height: 1)

transcriptionScrollView.scrollRectToVisible(rect, animated: true)

That should scroll you to the bottom of the label, assuming that your label is a subview of your scrollView. If not, you will need to convert the label's frame to the scrollView coordinate space.

Remember to call view.layoutIfNeeded() after updating the text in your label and before scrolling.

Programmatically scroll a UIScrollView

You can scroll to some point in a scroll view with one of the following statements in Objective-C

[scrollView setContentOffset:CGPointMake(x, y) animated:YES];

or Swift

scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

See the guide "Scrolling the Scroll View Content" from Apple as well.

To do slideshows with UIScrollView, you arrange all images in the scroll view, set up a repeated timer, then -setContentOffset:animated: when the timer fires.

But a more efficient approach is to use 2 image views and swap them using transitions or simply switching places when the timer fires. See iPhone Image slideshow for details.

Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift

Here's an extension I ended up writing.

Usage:

Called from my viewController, self.scrollView is an outlet to the UIScrollView and self.commentsHeader is a view within it, near the bottom:

self.scrollView.scrollToView(self.commentsHeader, animated: true)

Code:

You only need the scrollToView method, but leaving in scrollToBottom / scrollToTop methods too as you'll probably need those too, but feel free to delete them.

extension UIScrollView {

// Scroll to a specific view so that it's top is at the top our scrollview
func scrollToView(view:UIView, animated: Bool) {
if let origin = view.superview {
// Get the Y position of your child view
let childStartPoint = origin.convertPoint(view.frame.origin, toView: self)
// Scroll to a rectangle starting at the Y of your subview, with a height of the scrollview
self.scrollRectToVisible(CGRect(x:0, y:childStartPoint.y,width: 1,height: self.frame.height), animated: animated)
}
}

// Bonus: Scroll to top
func scrollToTop(animated: Bool) {
let topOffset = CGPoint(x: 0, y: -contentInset.top)
setContentOffset(topOffset, animated: animated)
}

// Bonus: Scroll to bottom
func scrollToBottom() {
let bottomOffset = CGPoint(x: 0, y: contentSize.height - bounds.size.height + contentInset.bottom)
if(bottomOffset.y > 0) {
setContentOffset(bottomOffset, animated: true)
}
}

}

Cannot scroll to bottom of UIScrollView after manually added items to view

Your last question/point is correct, you should use auto layout if you're using auto-layout in the Storyboard.

Luckily, UIStackView works great with auto layout, so if you change the yellow view to be a UIStackView and make an outlet to that in code, you can just add views:

stackView.addArrangedSubview(sensorLabel)

A short explanation of why it won't let you scroll without updating auto-layout:

Your yellow view is pinned to the labels that are inside it, and the bottom of the yellow view is also pinned to the bottom of the scroll view in the Storyboard.

Unless you increase the size of the yellow view (by changing which label the bottom of the yellow view is pinned to), the scroll view content size won't change either because it's pinned to the bottom of the yellow view. A stack view will do most of this work for you, so you just need to pin the bottom of the stack view to the bottom of the scroll view in the Storyboard.



Related Topics



Leave a reply



Submit