0

How to create an overlay above a UIButton in iOS?

In my projects there are 6 UIButtons.

For each button I need to have an overlay instruction above that UIButton, means both UIButton and overlay should be visible.

Once a user clicks on the overlay above the UIButton, overlay will remove/hidden and UIButton should be visible.

How can I achieve this?

rose lovely
  • 19
  • 1
  • 12

2 Answers2

0

UIButton is a subclass of UIView. This means, you can just use addSubview:.

[myButton addSubview:myOverlay];

To remove the overlay, add a gesture recognizer to the overlay.

dasdom
  • 13,975
  • 2
  • 47
  • 58
0

I will show you how to create an overlay for one button and remove it.

#define TAG 1234    //random number to identify the view overlaying the UIButton
UIView *view = [[UIView alloc] initWithFrame:button.bounds];
view.backgroundColor = [UIColor grayColor];
view.tag = TAG;
[button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button addSubview:view];

write this outside viewDidLoad method in .m file of your view controller

- (IBAction)btnClicked:(id)sender
{
   //removing overlay on button click
   UIView *view = [sender viewWithTag:TAG];
   [view removeFromSuperview];
}

I hope this solves your problem.

MrDank
  • 2,020
  • 2
  • 17
  • 39