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