Skip to content

Instantly share code, notes, and snippets.

@fdocr
Created August 30, 2016 20:05
Show Gist options
  • Save fdocr/0889971ce878b92e2f9963c224312571 to your computer and use it in GitHub Desktop.
Save fdocr/0889971ce878b92e2f9963c224312571 to your computer and use it in GitHub Desktop.
Extension for a UIView to find the first superview matching a certain class (or nil)
import UIKit
extension UIView {
func findSuperviewWithClass<T>(superViewClass : T.Type) -> UIView? {
var xsuperView : UIView! = self.superview!
var foundSuperView : UIView!
while (xsuperView != nil && foundSuperView == nil) {
if xsuperView.self is T {
foundSuperView = xsuperView
} else {
xsuperView = xsuperView.superview
}
}
return foundSuperView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment