Swift UIView Common Add method

Source: Internet
Author: User

There are several common ways to add UIView Swift

    Func Insertsubview (View:uiview, Atindex index:int)    func addsubview (view:uiview)    func insertsubview (view: UIView, Belowsubview Siblingsubview:uiview)    func insertsubview (View:uiview, Abovesubview Siblingsubview:uiview)


First, the most common addsubview is the normal addition

        Let View1=uiview (Frame:cgrectmake (Ten, (), ()) Let        View2=uiview (Frame:cgrectmake        ) View1.backgroundcolor=uicolor.redcolor ()        View2.backgroundcolor=uicolor.greencolor (                ) Self.view.addSubview (View1)        Self.view.addSubview (VIEW2)
Let's see the effect.




Parsing let's take a look at Self.view's sub-view and then we know the index of the two views we just added

        var arr:[anyobject]        arr = self.view.subviews;        println ("arr=%d", Arr.count)

The result is 4, then View1 index is 2,VIEW2 index is 3



Let's take a look at this method.

Insertsubview (view:UIView, atindex index: Int)

Add View to

        Let Blueview=uiview (Frame:cgrectmake () (+,,,)        Blueview.backgroundcolor=uicolor.bluecolor ()        Self.view.insertSubview (Blueview, Atindex:3)

The effect is as follows

We can see that Blueview was added between View1 and View2.

All that means is to add the view to the specified location.



Come down to us two ways to compare

    Func Insertsubview (View:uiview, Belowsubview siblingsubview:uiview)    func insertsubview (View:uiview, Abovesubview Siblingsubview:uiview)

        Let Orangeview=uiview (Frame:cgrectmake ()        Orangeview.backgroundcolor=uicolor.orangecolor ()        Self.view.insertSubview (Orangeview, Belowsubview:blueview) let                Purpleview=uiview (Frame:cgrectmake (130,        Purpleview.backgroundcolor=uicolor.purplecolor ()        Self.view.insertSubview (Purpleview, Abovesubview:blueview)

The effect is as follows


We saw that he was talking about the new two view separately add the top and bottom of the Blueview


All right, let's look at it.

Apple Development Group: 414319235 Welcome to join the Welcome discussion question

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift UIView Common Add method

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.