ios - 如何编辑 UIScrollView 大小?

标签 ios swift xcode uiscrollview

我正在尝试构建这个:

example

但是,我有这个: example2

我还没有开始处理背景颜色或在选中时更改幻灯片的背景颜色。

我现在的错误是无法获得正确的尺寸(您可以看出,当我滚动到末尾时,它会停在幻灯片 2 处)。

如何使框架和幻灯片正确配合? (每张幻灯片宽度为188,高度为153)。

这是我的代码:

func setupSlideScrollView(slides : [Slide]) {
        scrollView.frame = CGRect(x: 0, y: 0, width: 200, height: 160)
        scrollView.contentSize = CGSize(width: 188 * CGFloat(slides.count), height: 153)
        scrollView.isPagingEnabled = true

        for i in 0 ..< 3 {
            slides[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: 188, height: 153)
            slides[i].backgroundColor = UIColor.red
            scrollView.addSubview(slides[i])
        }
    }

对于所有代码,我都遵循本教程(步骤 10+ 除外,因为我不需要动画):https://medium.com/@anitaa_1990/create-a-horizontal-paging-uiscrollview-with-uipagecontrol-swift-4-xcode-9-a3dddc845e92

最佳答案

您没有为 ScrollView 内的单元格或票证提供正确的框架。

var lastCellMaxX: CGFloat = 0
    var constantSpacingBetweenCell: CGFloat = 10

    for i in 0 ..< 3 {
        slides[i].frame = CGRect(x: lastCellMaxX, y: 0, width: 188, height: 153)
        slides[i].backgroundColor = UIColor.red
        scrollView.addSubview(slides[i])
        lastCellMaxX += 188 + constantSpacingBetweenCell
    }

我不明白什么是 view.frame,我认为这对您的情况也没有用。 存储 lastcellMaxX,即 cell.origin.x + cellwidth + spacingBetweenCell, 这将是为下一个 View 提供框架时的 x

关于ios - 如何编辑 UIScrollView 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55273402/

相关文章:

ios - 导入gradle插件生成的cocoapods时,Xcode显示快速的编译器错误

ios - UIImagePickerController 相机空白快照图像

php - Swift/PHP 如何使用 Alamofire 在 UITableView 中显示 mysql 数据(json)

ios - 不能只用名称填充 TableView

xcode - MacRuby + Interface Builder : How to display, 然后关闭,然后再次显示一个窗口

ios - UITabbarController firSTLy 选定的选项卡表现异常

xml - 在 Swift 中对 XML 元素进行分组

swift - 应用程序不更新我的位置

ios - 静态表不显示

ios - iPhone X : Incorrect launch-screen orientation used