Some common methods of managing Subview in UIView(2013-01-22 10:04:40)
Tags: ios subview it |
Category: About iOS Learning |
Some common methods of managing Subview in UIView
A UIView can contain many Subview (other UIView), and these Subview have a so-called hierarchical relationship, which is somewhat similar to the concept of a layer in a drawing software, and the following code shows several methods commonly used on the management layer (Subview). Its code is as follows.
The first is the new and removed Subview that are most commonly used by everyone.
- //Remove the Subview from the current UIView
- [Subview Removefromsuperview];
- //Add a subview for UIView
- [UIView Addsubview:subview];
Move the Subview forward or backward in the UIView, moving forward will cover the Subview of the lower layer, and the next move will be covered by the upper Subview.
- //Move Subview forward one layer (swap position with its previous layer)
- [UIView Bringsubviewtofront:subview];
- //Subview Move backward one layer (with its next layer swapped position)
- [UIView Sendsubviewtoback:subview];
Use the index in UIView to exchange the layer level of two Subview each other.
- //exchange of two layers
- [UIView Exchangesubviewatindex:indexa WITHSUBVIEWATINDEX:INDEXB];
Use the variable name of the Subview to get its index value in the UIView (index).
- //Get index
- Nsinteger index = [[UIView subviews] Indexofobject:subview name];
Add the Subview to the Nsinteger (TAG) so that they distinguish each other.
- //Plus notes
- [Subview Settag:nsinteger];
Finally, get all the Subview in the UIView, call this method to return a nsarray, and list the Subview in the order of the images in the example, and then list all the Subview in Root in the sample image.
- //Fetch all Subview under the UIView
- [UIView Subviews]
The difference between Addsubview and Insertsubview
Addsubview is the topmost layer that adds the view to all layers
Equivalent to setting the Atindex parameter of the Insertsubview to View.subviews count
That
[view Addsubview:oneview] = = [view Insertsubview:oneview atIndex:view.subviews Count]
Addsubview is added to the last
Insertsubview is added to the specified location
If you want to remove all the sub-views of a UIView , the SDK does not have a method such as remove all. You can use the For Loop loop to call –removefromsuperview to remove
For example:
For (UIView *view in [Self.view Subviews])
{
[View Removefromsuperview]
}
iOS Development Diary 9-common methods for managing Subview in some UIView