iOS Development Diary 9-common methods for managing Subview in some UIView

Source: Internet
Author: User

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.

    1. //Remove the Subview from the current UIView
    2. [Subview Removefromsuperview];
    3. //Add a subview for UIView
    4. [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.

    1. //Move Subview forward one layer (swap position with its previous layer)
    2. [UIView Bringsubviewtofront:subview];
    3. //Subview Move backward one layer (with its next layer swapped position)
    4. [UIView Sendsubviewtoback:subview];

Use the index in UIView to exchange the layer level of two Subview each other.

    1. //exchange of two layers
    2. [UIView Exchangesubviewatindex:indexa WITHSUBVIEWATINDEX:INDEXB];

Use the variable name of the Subview to get its index value in the UIView (index).

    1. //Get index
    2. Nsinteger index = [[UIView subviews] Indexofobject:subview name];

Add the Subview to the Nsinteger (TAG) so that they distinguish each other.

    1. //Plus notes
    2. [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.

    1. //Fetch all Subview under the UIView
    2. [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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.