水平滚动视图不滚动

Posted

技术标签:

【中文标题】水平滚动视图不滚动【英文标题】:Horizontal scrollView does not scroll 【发布时间】:2015-03-02 11:48:45 【问题描述】:

我在互联网上关注了一个教程,解释了如何使用分页制作滚动视图。

当我只想显示 1 张图片时,它可以工作。但我需要 3 个标签和一个图像。我已经把所有东西都放在我的故事板上了。但是在设备/模拟器上运行时,我无法滚动。

//Scrollview
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var pageControl: UIPageControl!
var pageImages: [UIImage] = []
var pageViews: [UIImageView?] = []

@IBOutlet weak var blogImage: UIImageView!
@IBOutlet weak var blogTussenTitel: UILabel!
@IBOutlet weak var blogTitel: UILabel!
@IBOutlet weak var blogbeschrijving: UILabel!


var items: [String] = ["We", "Heart", "Swift"]

override func viewDidLoad() 
    super.viewDidLoad()
    self.sideMenuController()?.sideMenu?.delegate = self;
   if isMenuOpen() == true  disableObjects()  else  enableObjects() 

        // SrollView
    // 1
    pageImages = [UIImage(named: "photo1.png")!,
        UIImage(named: "photo2.png")!,
        UIImage(named: "photo3.png")!,
        UIImage(named: "photo4.png")!,
        UIImage(named: "photo5.png")!]

    let pageCount = pageImages.count

    // 2
    pageControl.currentPage = 0
    pageControl.numberOfPages = pageCount

    // 3
    for _ in 0..<pageCount 
        pageViews.append(nil)
    

    // 4
    let pagesScrollViewSize = scrollView.frame.size
    scrollView.contentSize = CGSize(width: pagesScrollViewSize.width * CGFloat(pageImages.count),
        height: pagesScrollViewSize.height)

    // 5
    loadVisiblePages()



func loadVisiblePages() 
    // First, determine which page is currently visible
    let pageWidth = scrollView.frame.size.width
    let page = Int(floor((scrollView.contentOffset.x * 2.0 + pageWidth) / (pageWidth * 2.0)))
    // Update the page control
    pageControl.currentPage = page
    // Work out which pages you want to load
    let firstPage = page - 1
    let lastPage = page + 1
    // Purge anything before the first page
    for var index = 0; index < firstPage; ++index 
        purgePage(index)
    
    // Load pages in our range
    for index in firstPage...lastPage 
        loadPage(index)
    
    // Purge anything after the last page
    for var index = lastPage+1; index < pageImages.count; ++index 
        purgePage(index)
    


func loadPage(page: Int) 
    if page < 0 || page >= pageImages.count 
        // If it's outside the range of what you have to display, then do nothing
        return
    
    // 1
    if let pageView = pageViews[page] 
        // Do nothing. The view is already loaded.
     else 
        // 2
        var frame = scrollView.bounds
        frame.origin.x = frame.size.width * CGFloat(page)
        frame.origin.y = 0.0
        // 3
        let newPageView = UIImageView(image: pageImages[page])
        newPageView.contentMode = .ScaleAspectFit
        newPageView.frame = frame
        scrollView.addSubview(newPageView)
        //Image inladen
        //self.blogImage.image = pageImages[page]
        // 4
        pageViews[page] = newPageView
    


func purgePage(page: Int) 
    if page < 0 || page >= pageImages.count 
        // If it's outside the range of what you have to display, then do nothing
        return
    
    // Remove a page from the scroll view and reset the container array
    if let pageView = pageViews[page] 
        pageView.removeFromSuperview()
        pageViews[page] = nil
    



func scrollViewDidScroll(scrollView: UIScrollView) 
    loadVisiblePages()

谁能解决问题? 提前致谢! 亲切的问候,汉内斯

【问题讨论】:

【参考方案1】:

确保您UIScrollView height 不超过View height 本身。

UIScrollView 设置

查看设置

ViewController.swift

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate 

    @IBOutlet weak var scroller: UIScrollView!

    override func viewDidLoad() 
        super.viewDidLoad()

        // Do any additional setup after loading the view.


        scroller.contentSize = CGSizeMake(400, 2300)
        scroller.showsHorizontalScrollIndicator = true
        scroller.scrollEnabled = true

    


【讨论】:

以上是关于水平滚动视图不滚动的主要内容,如果未能解决你的问题,请参考以下文章

滚动视图不水平滚动

水平滚动视图不滚动

(垂直)滚动视图包含另一个(水平)滚动视图 - Kivy

ios 8 - 水平滚动视图中的按钮拦截平移事件 - 滚动不起作用

我的水平滚动视图不起作用

如何与作为滚动视图子视图的水平滚动视图交互?