manuelBetancurt
manuelBetancurt

Reputation: 16138

ios change subview index runtime

i intend to do some menu with tabs,

if I have the tabs ordered with z index, and i insert each tab with:

insertSubview:(UIView *) atIndex:(NSInteger)

and by pressing a button on top of them I want to change the tab shown, on top

how can I change the subview index at runtime?

thanks!

Upvotes: 14

Views: 16217

Answers (3)

cweinberger
cweinberger

Reputation: 3588

Just insert at new index:

[containerView insertSubview:subview atIndex:newIndex];

Upvotes: 15

Esqarrouth
Esqarrouth

Reputation: 39181

Swift versions for easy copy paste:

insertSubview(myView1, belowSubview: myView2)

insertSubview(myView2, aboveSubview: myView1)

insertSubview(view: myView1, atIndex: 1)

Upvotes: 4

JustSid
JustSid

Reputation: 25318

sendSubviewToBack: and bringSubviewToFront: are the methods you are looking for. Another possibility is exchangeSubviewAtIndex:withSubviewAtIndex: if you want to exchange the layer of two views.

Upvotes: 47

Related Questions