jxdwinter
jxdwinter

Reputation: 2369

How can I insert a subview below the other subviews

everyone.

I have some labels that I draw them in the xib file, and add a background view using codes,but the background view is in the front of these labels, so I cant see them. So, my question is how can I add a background view below these labels.

Thank you in advance.

Upvotes: 40

Views: 30458

Answers (6)

rajeswari ratala
rajeswari ratala

Reputation: 720

https://medium.com/swift-programming/dynamic-layouts-in-swift-b56cf8049b08

Use VerticalLayout as the parent. it is quite simple and effective

Upvotes: 0

Kwaku Eshun
Kwaku Eshun

Reputation: 149

You can call this method in Swift 3

view.insertSubview(yourSubView, at: 0)

or

view.sendSubview(toBack: yourSubView)

Upvotes: 6

Ahmadreza
Ahmadreza

Reputation: 7222

Swift 3 and later.

view.insertSubview(theViewYouWantToAdd, at: 0)

Upvotes: 3

Paul.s
Paul.s

Reputation: 38728

If you Google UIView you'll end up at the documentation - UIView Class Reference

Then you need to look down the list of methods until you find something that sounds like it will roughly fit your needs like these two:

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index

and

- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview

At this point you just have to read the descriptions of how the methods behave and pick whichever one is easiest to implement for your design. It's really good for your own learning if you try to find the answer yourself before you ask a question

Upvotes: 63

QED
QED

Reputation: 9933

In IB the subviews of a view are listed in a hierarchy under that view. Subviews which appear above their siblings in that hierarchy will appear below them in the rendered view. So in the example below, the in 99:99 AM label appears under the Sign In button at runtime.

If your background is just a UIImageView, consider adding the view to your NIB and then setting its UIImage at runtime. That way, you don't get confused. Also, what Paul.s said.

enter image description here

Upvotes: 2

sch
sch

Reputation: 27536

You can use the method insertSubview:atIndex:

[self.view insertSubview:backgroundView atIndex:0];

Upvotes: 44

Related Questions