Skip to content

Instantly share code, notes, and snippets.

@gkye
Forked from santoshrajan/LayoutExample2.swift
Created March 20, 2016 03:34
Show Gist options
  • Save gkye/63b3ae29a12109bf7f09 to your computer and use it in GitHub Desktop.
Save gkye/63b3ae29a12109bf7f09 to your computer and use it in GitHub Desktop.
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 {
var 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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment