Search code examples
iosipadmodal-dialoguiactionsheet

How do I make the action sheet make the surrounding view darker when it's called?


The action sheet naturally greys out the background view a little bit, but it isn't really noticeable. I want to make it much darker.

Also, how do I make it so you cannot click anywhere else? Right now if you click outside of the action sheet, it makes the action sheet go away. I want that to not happen - you should have to push the "cancel" button to make the action sheet go away.


Solution

  • You may try something like this:

    @property (nonatomic, strong) UIView *shadowView; 
    
    - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
    {
        if (!self.shadowView)
        {
            self.shadowView = [[UIView alloc]initWithFrame:self.view.bounds];
            self.shadowView.backgroundColor = [UIColor colorWithRed:(42.0/255.0) green:(42.0/255.0) blue:(42.0/255.0) alpha:1.0];
            self.shadowView.alpha = 0.7;
            [self.view addSubview:self.shadowView];
        }
        else
        {
            self.shadowView.alpha = 0.7;
        }
    }
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        self.shadowView.alpha = 0.0;
    }