久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

以編程方式將 UIScrollView 滾動到 Swift 中的子 UIV

Programmatically scroll a UIScrollView to the top of a child UIView (subview) in Swift(以編程方式將 UIScrollView 滾動到 Swift 中的子 UIView(子視圖)的頂部)
本文介紹了以編程方式將 UIScrollView 滾動到 Swift 中的子 UIView(子視圖)的頂部的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我的 UIScrollView 中有一些屏幕內(nèi)容,它們只能垂直滾動.

I have a few screens worth of content within my UIScrollView which only scrolls vertically.

我想以編程方式滾動到包含在其層次結(jié)構(gòu)中某處的視圖.

I want to programmatically scroll to a view contained somewhere in it's hierarchy.

UIScrollView 移動,以便子視圖位于 UIScrollView 的頂部(動畫與否)

The UIScrollView move so that the child view is at the top of the UIScrollView (either animated or not)

推薦答案

這是我最后寫的一個擴(kuò)展.

Here's an extension I ended up writing.

用法:

從我的 viewController 調(diào)用,self.scrollView 是 UIScrollView 的出口,self.commentsHeader 是其中的一個視圖,靠近底部:

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)

代碼:

您只需要 scrollToView 方法,但也保留 scrollToBottom/scrollToTop 方法,因為您可能也需要這些方法,但感覺可以隨意刪除.

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)
        }
    }

}

這篇關(guān)于以編程方式將 UIScrollView 滾動到 Swift 中的子 UIView(子視圖)的頂部的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的邊緣工作) - IT屋-程序員軟件開發(fā)技術(shù)分享社
ImageView Scaling when scrolling down(向下滾動時 ImageView 縮放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的邊界自動更改,帶有內(nèi)容插圖)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干擾按鈕.怎么修?)
Handling scroll views with (custom, interactive) view controller presentation and dismissal(使用(自定義、交互式)視圖控制器呈現(xiàn)和解除處理滾動視圖)
主站蜘蛛池模板: 偷拍亚洲色图 | 国产精品一区二区三区在线 | 黄页网址在线观看 | 久草综合在线视频 | 国产精品久久久久久婷婷天堂 | 日韩久久久久久 | 午夜精品网站 | 亚洲欧美成人 | 性网站免费 | 中文字幕精品一区 | www.青青草 | 国产精品国产三级国产aⅴ原创 | 欧美日本韩国一区二区三区 | 999久久久免费精品国产 | 亚洲一区二区久久 | 成人精品国产 | 亚洲一区二区网站 | 欧美成人激情视频 | 在线播放亚洲 | 精品亚洲一区二区三区 | 日韩高清在线观看 | 国产精品一区二区不卡 | 美女一级毛片 | 久久精品小视频 | 91成人午夜性a一级毛片 | 在线观看视频91 | 久久久国 | 亚洲综合中文字幕在线观看 | 极品的亚洲 | 中国美女撒尿txxxxx视频 | 午夜视频网站 | 天天操天天干天天透 | 一区二区三区在线播放 | 羞羞视频免费观看入口 | 久久久久久91香蕉国产 | 天天干夜夜| 青青草原综合久久大伊人精品 | 国产美女在线观看 | 亚洲一区二区三区免费观看 | 美女视频. | 天天爱天天操 |