Configuring UIAlertController and UIAlertControllerStyleActionSheet

I am trying to create a custom UIAlertController with the UIAlertControllerStyleActionSheet (formerly UIActionSheet) style , and I ran into such difficulties just setting it up. I want my UIAlertAction to contain the image on the left and reduce the size of the buttons .

I know this is possible:

enter image description here

(exactly what I'm looking for)

I searched for a few hours, but I cannot find anyone on the Internet that can help me achieve this simple task. Also, since the UIActionSheet has been deprecated since iOS 9, anytime I try to find a solution that it is no longer in use. Also, I read that UIAlertController is not intended to subclass ...

- , ? UIView, ?

+4
2

API/. , , UIAlertController. , UIAlertContoller . UIAlertController iOS 8. Github. , . , , , :

UIAlertController

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Alert Title" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];

! .

NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... StackOverFlow!"];
[hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30.0] range:NSMakeRange(24, 11)];
[alertVC setValue:hogan forKey:@"attributedTitle"];

UIAlertAction, , .

UIAlertAction *button = [UIAlertAction actionWithTitle:@"First Button"
                                                 style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction *action){
                                                   //add code to make something happen once tapped
                                               }];
UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Second Button"
                                                 style:UIAlertActionStyleDefault
                                               handler:^(UIAlertAction *action){
                                                   //add code to make something happen once tapped
                                               }];

AlertAction. UIImageRenderingModeAlwaysOriginal

[button setValue:[[UIImage imageNamed:@"image.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forKey:@"image"];

[alertVC addAction:button2];
[alertVC addAction:button];

ViewController

[self presentViewController:alertVC animated:YES completion:nil];

enter image description here

+3

, , , :

, , UIAlertController, . ViewController , :

  • UIView, , ~ 0,7. ( : , , )

  • UIButtons .

  • , .

:

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

, , , :

  • [UIView animateWithDuration: animations: ^ {}];, .
  • : UIButtons foregroundView ( foregroundLayer), 0.7 alpha,
  • foregroundView, cancelButton: . , , AlertView.
0

Source: https://habr.com/ru/post/1661184/


All Articles