關於addSubView需要注意的事項 -今天吃了一個大虧,addsubview大虧

來源:互聯網
上載者:User

關於addSubView需要注意的事項 -今天吃了一個大虧,addsubview大虧

addSubview:

Adds a view to the end of the receiver’s list of subviews.

譯:增加一個視圖到接收者的子視圖列表中。

 

- (void)addSubview:(UIView *)view

Parameters

view

The view to be added. This view is retained by the receiver. After being added, this view appears on top of any other subviews.

譯:view參數代表被增加的view,這個view會被它的接收者retain一次(即引用計數+1)。增加完成之後,這個view將出現在接收者的其他子視圖的上面。

ps:關於子視圖的出現的層次的問題,可以從這些子視圖被儲存的資料結構來探尋答案 ,每個視圖都有個數組的屬性,subviews,這個就是儲存的它的子視圖的引用。而這個數組的順序就是代表了各個子視圖被加入時的順序。index=0的就是最先被加入進去的,以此類推。所以,索引值越高的視圖越不容易被覆蓋。

Discussion

This method retains view and sets its next responder to the receiver, which is its new superview.

譯:這個方法會retain一次view,並且設定它的下一個響應者是receiver,即它的新的父視圖。

ps:在removeFromSuperview裡已經說過,其實視圖直接的操作往往牽涉到兩個方面的操作,一個是視圖的資料結構,一個是響應者鏈。當然,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.

譯:每一個視圖只能有唯一的一個父視圖。如果當前操作視圖已經有另外的一個父視圖,則addsubview的操作會把它先從上一個父視圖中移除(包括響應者鏈),再加到新的父視圖上面。


總結: 把兩個記憶體相同的View,addSubview一個View上,最後不是兩個View的疊加,而是就是加上了一層!

 

Availability

  • Available in iOS 2.0 and later.

See Also

    • – insertSubview:atIndex:
    • – insertSubview:aboveSubview:
    • – insertSubview:belowSubview:
    • – exchangeSubviewAtIndex:withSubviewAtIndex:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.