Skip to content

Instantly share code, notes, and snippets.

@helmutgranda
Forked from santoshrajan/LayoutExample2.swift
Last active February 17, 2016 08:25
Show Gist options
  • Save helmutgranda/6984678af6fb102b8317 to your computer and use it in GitHub Desktop.
Save helmutgranda/6984678af6fb102b8317 to your computer and use it in GitHub Desktop.
import UIKit
class VerticalLayout: UIView {
var yOffsets: [CGFloat] = []
init(width: CGFloat) {
super.init(frame: CGRectMake(0, 0, width, 0))
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
var height: CGFloat = 0
for i in 0..<subviews.count {
let view = subviews[i] as UIView
view.layoutSubviews()
height += yOffsets[i]
view.frame.origin.y = height
height += view.frame.height
}
self.frame.size.height = height
}
override func addSubview(view: UIView) {
yOffsets.append(view.frame.origin.y)
super.addSubview(view)
}
func removeAll() {
for view in subviews {
view.removeFromSuperview()
}
yOffsets.removeAll(keepCapacity: false)
}
}
@helmutgranda
Copy link
Author

Adding "import UIKit" otherwise you will get a "Use of undeclared type 'UIView'" error as well as changing "var i" to "let i"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment