-
-
Save helmutgranda/6984678af6fb102b8317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adding "import UIKit" otherwise you will get a "Use of undeclared type 'UIView'" error as well as changing "var i" to "let i"