【发布时间】:2016-10-04 11:02:58
【问题描述】:
我正在尝试在我的滚动视图中垂直对齐我的图像。不幸的是,我无法弄清楚问题所在。我认为这是数学上的东西,这不是我最擅长的事情。 :(
我希望有人可以帮助我解决这个问题。我还将在下面提供我的代码和问题的图像:
//
// 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
}
}
非常感谢!
【问题讨论】:
-
没有得到您的问题,提供的图像有什么问题?
-
@BharatModi 图像需要位于前 0 位置。目前,它们的位置略低于框架顶部。
-
你对 imageView 设置了哪些约束,你的实际要求是什么,如果图像数量是动态的并且你需要它们垂直滚动,那么你为什么不使用 tableView?
-
我建议不要手动设置滚动的高度/内容大小,让自动布局根据里面的内容自行计算。
-
@BharatModi 将约束设置为屏幕的所有边缘。所以它适用于所有尺寸的手机。图像计数是动态的。如果可能的话,我想为此目的使用滚动视图。 :D
标签: ios swift xcode uiscrollview