UIAlertController and UIAlertControllerStyleActionSheet customization

前端 未结 2 503
耶瑟儿~
耶瑟儿~ 2021-01-06 04:47

I\'m trying to create a custom UIAlertController with style UIAlertControllerStyleActionSheet (formerly UIActionSheet) and I\'m encounterin

2条回答
  •  情书的邮戳
    2021-01-06 05:41

    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 :

    1. 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)

    2. Create UIButtons and place it manually in the current view.

    3. 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 :

    • Using [UIView animateWithDuration: animations:^{}]; i was able to gradually obscure the foreground view and to make the cancelbutton showing up from the bottom of the screen.
    • Note: you cannot add UIButtons directly to the foregroundView (here called foregroundLayer), otherwise they will herit of the 0.7 alpha we set earlier
    • I added a gesture recognizer selector to the foregroundView that has the same effect of the cancelButton selector : it remove all created objects from superview. This way, when the user clicks outside the edges of the list, it closes the custom AlertView.

提交回复
热议问题