iOS: 神奇的addSubView

來源:互聯網
上載者:User

標籤:

看著addSubView, 本以為是添加多個對象, 但通過測試代碼, 發現同一個對象在addSubView中只會添加一次. 想想, 視圖對象是通過引用得到的. 在視圖的子視圖集中, 只儲存一個相應的對象, 才不會造成系統的混亂.

    override func viewDidLoad() {        super.viewDidLoad()                var view2 = UIView(frame: CGRectZero)                //重複添加多次        for i in 1...50{            view.addSubview(view2)        }                //結果, 只添加了一次        //view.subviews中只有三項, 前兩項是_UILayoutGuide        println(view.subviews.count)        for i in view.subviews{            println(i)        }    }

 查看addSubView的協助, 找到了原因:

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

視圖有且只有一個父視圖. 如果視圖已經有一個父視圖並且這個父視圖不是將要添加的視圖, 本方法會把視圖原有的父視圖移除, 將新視圖設定為父視圖.

iOS: 神奇的addSubView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.