I\'m trying to create a custom UIAlertController with style UIAlertControllerStyleActionSheet (formerly UIActionSheet) and I\'m encounterin
For those who could be interested in how I dit to overcome this, I post my solution:
I finally gave up on UIAlertController, which is not intended to be subclassed. Instead, in my ViewController, I created a method that programmatically re-create the same effect and rendering :
Create an UIView that occupies the whole screen an set it's alpha to ~0.7. (This has two objectives, a esthetical one and a physical one the hide the current view, avoiding buttons behind the custom Alert to be clicked)
Create UIButtons and place it manually in the current view.
Add this foregroundView and buttons to the current view and when button is clicked simply remove it.
Example code :
-(void) displayAlertChoice{
// create foreground layer to hide current view
foregroundLayer = [[UIView alloc] initWithFrame:CGRectMake(0,0,screenWidth,screenHeight)];
foregroundLayer.backgroundColor = [UIColor blackColor];
foregroundLayer.alpha = 0.0;
[self.view addSubview:foregroundLayer];
[UIView animateWithDuration:0.2 animations:^{foregroundLayer.alpha = 0.7;}]; // animation de son apparition (alpha)
// Hide foreground layer when clicked
UITapGestureRecognizer *tapForegroundLayer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickForegroundLayer:)];
[foregroundLayer addGestureRecognizer:tapForegroundLayer];
// CancelButton
cancelButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[cancelButton setFrame:CGRectMake(marginWidth,
2*screenHeight - buttonHeight - marginHeight, // OFF SCREEN
buttonWidth,
buttonHeight)];
[cancelButton setTitle:@"Annuler" forState:UIControlStateNormal];
[cancelButton addTarget:self action:@selector(alertCancelButton:) forControlEvents:UIControlEventTouchUpInside];
[cancelButton setBackgroundColor:[UIColor whiteColor]];
cancelButton.layer.cornerRadius = 8;
[self.view addSubview:cancelButton];
[UIView animateWithDuration:0.2 animations:^{ // animation de son apparition (fram up)
[cancelButton setFrame:CGRectMake(marginWidth,
screenHeight - buttonHeight - marginHeight, // ON SCREEN
buttonWidth,
buttonHeight)];
}];
}
This code is a bit more complex than just adding view and buttons, in order to recreate animation when the Alert showes up :