hanumanDev
hanumanDev

Reputation: 6614

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

Upvotes: 4

Views: 3633

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130193

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.

Upvotes: 3

Paras Joshi
Paras Joshi

Reputation: 20541

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];
}

Upvotes: 6

Related Questions