vk2015
vk2015

Reputation: 1138

How can I bring a view in front of another view, in Swift?

Below are how my views are organized in IB, top->bottom when the app is started.

The user can do something to make "Category Table View Header" temporarily expand over "Name View" - however once doing so, the .TouchDown action assigned to "Category Table View Header" no longer works wherever it overlaps with "Name View" (i.e., the user can tap the header anywhere it doesn't overlap with name view and it still works).

enter image description here

I know that may be confusing, so I drew out some boxes. On the left is the original, right is after user action - problem is on the right the action on the red box only works if the user taps the bottom half, not the top half.

enter image description here

My guess is its because the header is lower in the view hierarchy than the name view, but it would be hard for me to change that without messing around with a bunch of constraints.

I also tried setting nameView.hidden = true, but that doesn't work.

Upvotes: 41

Views: 107184

Answers (6)

Deepak Ghadi
Deepak Ghadi

Reputation: 183

bringSubViewToFront did not work in my case, Take a look

enter image description here

As I wanted this notification count comes before image, bringSubViewToFront did not work, So I just drag this label below to stack view of my imgae and bottom label, it worked for me. I hope this will work for all

Upvotes: -1

Mudith Chathuranga Silva
Mudith Chathuranga Silva

Reputation: 7434

If you want to bring a subview to the front, you can use:

SWIFT 4 + UPDATE

self.view.bringSubviewToFront(yourView)

SWIFT 3 UPDATE

self.view.bringSubview(toFront: yourView)

Send view to back:-

SWIFT 4+ UPDATE

self.view.sendSubviewToBack(yourView)

SWIFT 3 UPDATE

self.view.sendSubview(toBack: yourView)

SWIFT 4+ UPDATE - INSERT VIEW ON SPECIFIC LOCATION IN THE STACK

 parentView.insertSubview(yourView, belowSubview: requiredViewOnStack)
 parentView.insertSubview(yourView, aboveSubview: requiredViewOnStack)

Upvotes: 82

Visal Rajapakse
Visal Rajapakse

Reputation: 2042

Swift 5.4

views in UIKit are drawn from the back towards the front (like adding icing onto a cake). This means that the new views will be placed on top/over the previously placed views. (e.g. Icing is the new view and the base of the cake being the old view.

You can use the following to manipulate the views Z-position using,

  1. Forward
parent.bringSubviewToFront(child)
  1. Backward
parent.sendSubviewToBack(child)

Upvotes: 1

Vinoth
Vinoth

Reputation: 9734

In Swift 5

To bring a subview to front

self.view.bringSubviewToFront(yourView)

To send a subview to back

self.view.sendSubviewToBack(yourView)

Upvotes: 11

K-A
K-A

Reputation: 67

The method has been updated since swift 3

What has worked for me and hopefully for you is using :

YourView.bringSubview(toFront: yourelementA)
YourView.bringSubview(toFront: yourelementB)

Alternatively you could use the following to send the object that is in the front:

YourView.sendSubview(toBack: yourelementC)

I hope this helps

Upvotes: 1

tosha
tosha

Reputation: 168

You can take a control over the order of subviews using methods: bringSubviewToFront and sendSubviewToBack from the superview.

You can access all the subviews contained by superview using self.view.subview array.

Upvotes: 4

Related Questions