도래울

remove all subviews of a view in Swift 본문

개발/iOS

remove all subviews of a view in Swift

도래울 2016. 7. 13. 09:06

By far the best way to do this in Swift for iOS is:

view.subviews.forEach({ $0.removeFromSuperview() }) // this gets things done
view.subviews.map({ $0.removeFromSuperview() }) // this returns modified array

^^ These features are fun!

let funTimes = ["Awesome","Crazy","WTF"]
extension String { 
    func readIt() {
        print(self)
    }
}

funTimes.forEach({ $0.readIt() })

//// END EDIT

Just do this:

for view in self.view.subviews {
    view.removeFromSuperview()
}

Or if you are looking for a specific class

for view:CustomViewClass! in self.view.subviews {
        if view.isKindOfClass(CustomViewClass) {
            view.doClassThing()
        }
    }


'개발 > iOS' 카테고리의 다른 글

swift Get button click inside UI table view cell  (0) 2016.07.13
Loop through subview to check for empty UITextField - Swift  (0) 2016.07.13
swift for in loop  (0) 2016.07.13
swift uitableview checkbox example  (0) 2016.07.13
Swift string replace  (0) 2016.07.12
Comments