ios - ScrollView swift 3 中的垂直对齐图像

标签 ios swift xcode uiscrollview

我正在尝试在 ScrollView 中垂直对齐图像。不幸的是我无法弄清楚问题是什么。我认为这是数学方面的事情,这不是我最擅长的事情。 :(

希望有人能帮我解决这个问题。我还将提供以下问题的代码和图像:

//
//  muscleListVC.swift
//  ActiveRest
//
//  Created by Fhict on 04/10/16.
//  Copyright © 2016 Kevin Vugts. All rights reserved.
//

import UIKit

class muscleListVC: UIViewController {

    var images = [UIImageView]()

    @IBOutlet weak var scrollView: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()



        print("Count: \(images.count)")

    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        var contentHeight: CGFloat = 0.0
        let scrollHeight = scrollView.frame.size.height

        for x in 0...4 {

            let image = UIImage(named: "muscle\(x).png")
            let imageView = UIImageView(image: image)
            images.append(imageView)

            var newX: CGFloat = 0.0
            newX = scrollHeight / 4 + scrollHeight * CGFloat(x) / 4
            print("the size is \(newX)")
            contentHeight += newX

            scrollView.addSubview(imageView)
            imageView.frame = CGRect(x: 0, y: newX, width: view.frame.size.width, height: 166)

            print("the content height: \(contentHeight)")
            scrollView.contentSize = CGSize(width: scrollView.frame.size.width, height: contentHeight)

        }
        scrollView.clipsToBounds = false
    }
}

enter image description here

非常感谢! <3

最佳答案

我不太确定你打算做什么,但我尝试稍微修改你的代码以使其工作。

override func viewDidLoad() {

        super.viewDidLoad()

        let numberOfImages = 5
        let imageViewHeight:CGFloat = 166.0
        scrollView.contentSize = CGSize(width: view.frame.size.width, height: imageViewHeight*(CGFloat)(numberOfImages))

        var newY:CGFloat = 0

        for x in 0...(numberOfImages-1) {

            let image = UIImage(named: "muscle\(x).png")
            let imageView = UIImageView(image: image)
            images.append(imageView)
            imageView.frame = CGRect(x: 0, y: newY, width: view.frame.size.width, height: imageViewHeight)
            newY = newY + CGFloat(imageViewHeight)
            scrollView.addSubview(imageView)

        }


    }

关于ios - ScrollView swift 3 中的垂直对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39850615/

相关文章:

iOS 11 PDFKit Ink 注释 - 无法填充 UIBezierPath

iphone - iOS:查找具有相同标签的所有控件

ios - 无法将类型 'UITableViewCell' 的值转换为 '(AppName).(CustomCellName)'

ios - 在 UISearchController 下添加水平滚动过滤器选项

iphone - 实例变量到底是什么?为什么它们如此重要?

ios - Swift:如何在 cellForRowAtIndexPath 方法中设置 UITableViewCell

ios - 无法安装分发配置文件

ios - UITableViewCell backgroundView 未出现在设备上

ios - 自动布局以动态调整 UILabel 的高度和宽度

ios - segue 创建 View Controller 的新实例 swift