Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a UIButton to always be on top

I have a UIButton and a UIView that animates, sliding down when the UIButton is pressed. The problem is that the UIView obscures the UIButton. I'd like the UIButton to always remain on top.

I tried this in the viewDidLoad:

    [self.view bringSubviewToFront:categoryBtn];

doesn't work though.

thanks for any help

like image 724
hanumanDev Avatar asked Sep 15 '25 10:09

hanumanDev


2 Answers

After Add any UIView or any other view on your this mainview , after that just add your this line for brings as a supeview for button

[self.view bringSubviewToFront:categoryBtn];

For Example on button action event you add any view then use code like bellow..

-(IBAction)yourButton_Clicked:(id)sender
{

   ///Add some code or view here and then after write this line here see bellow line
   [self.view bringSubviewToFront:categoryBtn];
}
like image 131
Paras Joshi Avatar answered Sep 17 '25 01:09

Paras Joshi


The other answer is will work, but this is exactly the kind of thing "insertSubview: belowSubview" was added for.

[self.view insertSubview:myNewView belowSubview:_categoryBtn];

This allows you to add the new view below the button on the view stack instead of adding it above the button and then rearranging the views.

like image 23
Mick MacCallum Avatar answered Sep 16 '25 23:09

Mick MacCallum