Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@santoshrajan
Created November 7, 2014 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save santoshrajan/eb0988c460589f07beb5 to your computer and use it in GitHub Desktop.
Save santoshrajan/eb0988c460589f07beb5 to your computer and use it in GitHub Desktop.
class HorizontalLayout: UIView {
var xOffsets: [CGFloat] = []
init(height: CGFloat) {
super.init(frame: CGRectMake(0, 0, 0, height))
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
var width: CGFloat = 0
for i in 0..<subviews.count {
var view = subviews[i] as UIView
view.layoutSubviews()
width += xOffsets[i]
view.frame.origin.x = width
width += view.frame.width
}
self.frame.size.width = width
}
override func addSubview(view: UIView) {
xOffsets.append(view.frame.origin.x)
super.addSubview(view)
}
func removeAll() {
for view in subviews {
view.removeFromSuperview()
}
xOffsets.removeAll(keepCapacity: false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment