Last active
September 23, 2015 05:00
-
-
Save romainmenke/ade9e8108c656d67f463 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 | |
extension SubviewsCGRectUnion { | |
mutating func setCGRectUnionWithSubviews() { | |
frame = getCGRectUnionWithNestedSubviews(subviews: subviews, frame: frame) | |
fixPositionOfSubviews(subviews, frame: frame) | |
} | |
func getCGRectUnionWithSubviews() -> CGRect { | |
return getCGRectUnionWithNestedSubviews(subviews: subviews, frame: frame) | |
} | |
private func getCGRectUnionWithNestedSubviews(subviews subviews_I: [UIView], frame frame_I: CGRect) -> CGRect { | |
var rectUnion : CGRect = frame_I | |
for subview in subviews_I { | |
rectUnion = CGRectUnion(rectUnion, getCGRectUnionWithNestedSubviews(subviews: subview.subviews, frame: subview.frame)) | |
} | |
return rectUnion | |
} | |
private func fixPositionOfSubviews(subviews: [UIView], frame frame_I: CGRect) { | |
let frameFix : CGPoint = frame_I.origin | |
for subview in subviews { | |
subview.frame = CGRectOffset(subview.frame, -frameFix.x, -frameFix.y) | |
} | |
} | |
} | |
protocol SubviewsCGRectUnion { | |
var frame : CGRect { get set } | |
var subviews : [UIView] { get } | |
} | |
extension UIView : SubviewsCGRectUnion { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment