Created
November 7, 2014 17:47
-
-
Save santoshrajan/eb0988c460589f07beb5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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