Remove all the subviews from a UIScrollView?

前端 未结 5 1782
-上瘾入骨i
-上瘾入骨i 2021-02-02 06:08

How do I remove all of the subviews from a UIScrollview?

5条回答
  •  日久生厌
    2021-02-02 06:37

    Let scrollView be an instance of UIScrollView.

    In Objective-C, it's pretty easy. Just call makeObjectsPerformSelector:, like so:

    Objective-C:

    [scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    

    In Swift, you don't get that runtime access, so you have to actually handle the iteration yourself.

    Swift:

    A concise version, from here:

    scrollview.subviews.map { $0.removeFromSuperview() }
    

    A more descriptive way to do this (from here) assumes scrollview.subviews:

    let subviews = self.scrollView.subviews
    for subview in subviews{
        subview.removeFromSuperview()
    }
    

提交回复
热议问题