Skip to content

Instantly share code, notes, and snippets.

@santoshrajan
Last active August 13, 2020 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save santoshrajan/58b399b0b6a7a60a72d4 to your computer and use it in GitHub Desktop.
Save santoshrajan/58b399b0b6a7a60a72d4 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)
}
}
@Naliniamu
Copy link

Hi,
I am using this class for UIscrollview instead of UIview. While scrolling i am getting extra view added and also view getting shake. can u help me

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