16

I know there is a way of bringing a subview to the front of the hierarchy. However is there a way of bringing a button that i have placed on a story and calling a function that will make that button the top layer so its not hidden by any other elements that are added that aren't in the story board.

Thanks,

Fudgey
  • 3,793
  • 7
  • 32
  • 53

7 Answers7

13

I am late for this question, but not too late to answer it by Swift 3

Swift 3

 view.bringSubview(toFront: theButton)
Community
  • 1
  • 1
6

A UIButton is a UIView, so you can call -bringSubviewToFront: on your button instance.

Gereon
  • 17,258
  • 4
  • 42
  • 73
6

uiview's are displayed in the order they are stacked. One can change this to say were to insert the new uiview, which in your case is a uibutton.

One way of doing that is by creating a IBOUtlet of the uibutton and adding that button instance as a subview/newview above an existing uiview.

CODE:

[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];

Also if you are not able to figure out the order of the uiview , then you can check that in xcode

  • When running the project in debug mode under debug window there is a button(highlighted in below image) Debug View Hierarchy, you need to click that. (highlighted in below image)

Debug window tool bar

  • After that you will able to see all the views rendering on the screen at current instance, using this feature you will be able to understand were is your new uiview in the uiview stack order(shown below).

UI View Hierarchy

halfer
  • 19,824
  • 17
  • 99
  • 186
dreamweiver
  • 6,002
  • 2
  • 24
  • 39
  • I added a view named sideBar into view controller But that view controller is one of the tabBar Controller tabs I want move the sideBar in Front of the tabBar Controller But I can't do that here is my question link please help me to solve the problem https://stackoverflow.com/questions/48127021/how-to-place-uiview-infront-of-view-that-is-infront-of-tabbar-and-special-tabbar – Saeed Rahmatolahi Jan 09 '18 at 12:28
  • I would suggest you to check the stacking order of your uiView's and then move the ordering as required, you can refer to this So, https://stackoverflow.com/questions/1519141/difference-between-addsubview-and-insertsubview-in-uiview-class – dreamweiver Jan 09 '18 at 12:46
  • It's hard to explain can you chat ? I will explain that with photo and more codes – Saeed Rahmatolahi Jan 10 '18 at 05:48
  • I cant find you on stackexchange chat, https://chat.stackexchange.com/ go ahead and invite me , we can talk there – dreamweiver Jan 10 '18 at 06:34
  • ok so If I explain the problem in the comments the link of chat will appear – Saeed Rahmatolahi Jan 10 '18 at 07:02
  • the problem for me is that I have a tabBar and I want to place a view inFront of that But the problem is that I made that view in story board But I read the similar pages that If I want to do that I have to define a view in code and I didn't find that how can I place a view that made in story board in front of all things ( tab bar and views ) – Saeed Rahmatolahi Jan 10 '18 at 07:05
  • 1
    even with storyboard, there is a code behind that is generated for work flow what you might have created, just go through the code one step at a time & figure out were do you need to alter the code to bring corresponding view to front. you can refer to the "debugViewHierarchy" to know the stack order and update the code accordingly – dreamweiver Jan 11 '18 at 09:12
2

Swift

The following works great if the view you are working with is being hidden views that are in the same view.

superview?.bringSubviewToFront(self)
Community
  • 1
  • 1
user160917
  • 9,211
  • 4
  • 53
  • 63
1

Swift 5

In my case I had to insert my view behind some buttons. By insert it at the zero position it was placed behind the buttons.

        self.view.insertSubview(self.mapView!, at: 0)  
Dave Levy
  • 1,036
  • 13
  • 20
0

i have the same problem with you and a friend of mine helped me

i can bring my button to another subview using this function on your view controller where you declare your button.

    override func viewDidLoad() {
        super.viewDidLoad()
        UIApplication.shared.keyWindow?.addSubview(chatButton)
    }
dimaskpz
  • 81
  • 8
0
button.superview?.bringSubviewToFront(button)
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 30 '22 at 15:10