久久久久久久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)和解除處理滾動視圖)
主站蜘蛛池模板: 欧美亚洲国语精品一区二区 | 国产精品一区二 | 日韩中文字幕在线视频 | 日韩综合在线 | 国产精品视频中文字幕 | 精品动漫一区 | 免费在线日韩 | 久久久久久999 | 欧美日韩视频在线 | 中文字幕高清免费日韩视频在线 | 国产一级淫片免费视频 | 超碰婷婷| 国产精品久久久久久久久久三级 | 成人在线播放 | 免费视频成人国产精品网站 | 日本成人区| 免费看91 | 中文字幕一区二区不卡 | 国产午夜精品久久久久免费视高清 | 一级毛片色一级 | 免费在线看黄 | 亚洲自拍一区在线观看 | 精品视频久久久 | 亚洲成人精品一区 | 欧美日韩综合一区 | 欧美一级在线观看 | 国产一区二区在线播放视频 | 欧美国产日韩在线观看 | 中文字幕二区 | 蜜月aⅴ免费一区二区三区 99re在线视频 | 亚洲视频在线一区 | 99久久久99久久国产片鸭王 | 久久精品亚洲精品国产欧美kt∨ | 中文字幕一区二区三区精彩视频 | 亚洲精品视频一区二区三区 | aaa一区 | 欧美日韩一二三区 | 国产精品美女在线观看 | 国产精品欧美一区二区三区 | 波多野结衣中文字幕一区二区三区 | 欧美日韩看片 |