gpt4 book ai didi

ios - 水平 scrollView 不滚动

转载 作者:搜寻专家 更新时间:2023-11-01 05:41:47 25 4
gpt4 key购买 nike

我已经按照 Internet 上的教程进行操作,该教程解释了如何制作带分页的 ScrollView 。

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

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

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

最佳答案

确保您的 UIScrollView 高度 不超过 View 高度 本身。

UIScrollView 设置

enter image description here

查看设置

enter image description here

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

}

}

关于ios - 水平 scrollView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28809075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com