简体   繁体   中英

Pin content view in UIScrollView to its bottom rather than its top edge

A UIScrollView pins its content view by default to its top edge, ie when you add some content to a scroll view with a vertical scrolling axis and the content's total height is smaller than the height of the whole scroll view, the content is displayed at the top of the scroll view.

Is there a way to change this behavior and make the scroll view pin its content view to its bottom?

(In other words: I would like the content to "grow from the bottom".)


示例布局


it is possible to add a view to the scrollview in a certain position

this is simple example code:

class ViewController: UIViewController {

    var scrollView: UIScrollView!
    var views: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        views = UIView.init()
        scrollView = UIScrollView.init()


        scrollView.addSubview(views)
        self.view.addSubview(scrollView)

        addViewToBottomScroll()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        views.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 1300)
        scrollView.frame = self.view.frame
        scrollView.contentSize = views.frame.size
    }

    func addViewToScrollView() {
        let label = UILabel(frame: CGRect(x: 0, y: 10, width: self.view.frame.width, height: 40))
        label.text = "ScrollView"
        label.textAlignment = .center

        views.addSubview(label)

        let bottomView = UIView(frame: CGRect(x: 15.0, y: 950.0, width: self.view.frame.width - 30, height: 300))
        bottomView.backgroundColor = .yellow

        views.addSubview(bottomView)
    }
}

在此处输入图片说明

hopefully answer your question

The usual workaround is to flip the scroll view, then flip the content view.

eg

        scrollView.transform = CGAffineTransform(scaleX: 1, y: -1)
        contentView.transform = CGAffineTransform(scaleX: 1, y: -1)

I'd really like to know a cleaner solution myself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM