Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The right way for creating a custom popup / alert UIView / UIViewController

I've been developing for iOS for quite a while now, but I always got problems with custom alerts / popups, I just don't know what is the right way to add a custom view as a popup on top of all other views in app.

I've checked online and there are several ways to do it, but what is the best way for creating those custom alerts / popups?

There is a CustomViewController class and this is how I've created it and add it as a popup:

  1. Create a new UIViewController

  2. Add a UIView inside the UIViewController's XIB file as a child of self.view (This will hold the popup elements inside)

  3. Add this method inside the UIViewController for presenting the view:

    - (void)presentViewController
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.windowLevel = UIWindowLevelStatusBar;
        self.window.screen = [UIScreen mainScreen];
        [self.window makeKeyAndVisible];
    
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        [mainWindow setUserInteractionEnabled:NO];
        [self.window addSubview:self.view];
    
        self.view.alpha = 0.0f;
        self.backgroundImage.image = [self takeSnapshot];
    
        [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    
            self.view.transform = CGAffineTransformIdentity;
            self.view.alpha = 1.0f;
    
        } completion:nil];
    }
    
  4. Add this method inside the UIViewController for removing the view:

    - (void)dismissShareViewController
    {
        [UIView animateWithDuration:DEFAULT_ANIMATION_DURATION
                         animations:^{
                             self.view.alpha = 0.0f;
                         } completion:^(BOOL finished) {
                             [self.view removeFromSuperview];
                             self.window = nil;
                             [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
                             UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
                             [mainWindow setUserInteractionEnabled:YES];
                         }];
    }
    

Usage:

  1. Creating a property for the UIViewController

    @property (strong, nonatomic) CustomViewController *viewController;
    
  2. Initialize and present

    - (void)showCustomViewController
    {
        self.viewController = [[CustomViewController alloc] init];
        [self.viewController presentViewController];
    }
    

The problem is that I always get a Received Memory Warning. Also I've added a - (void)dealloc method inside the CustomViewController to know if the UIViewController is releasing properly, but for some reason the dealloc method is getting called right after the presentViewController and not after the dismissShareViewController as it should.

Another thing that I want to achieve: This CustomViewController has no UIStatusBar and when I try to present MFMailComposeViewController, I always get the MFMailComposeViewController to show without a UIStatusBar or with white status bar, but I can't change it to default back again. I've tried several question here on StackOverflow but without any luck, it just won't change.

Putting a UIView or UIWindow above Statusbar

Please, for once and for all, someone will help me with it.

like image 708
ytpm Avatar asked Mar 16 '23 16:03

ytpm


1 Answers

UIAlertView is a subclass of UIView. If you wanted to create a custom alert view, I would also subclass UIView so that it is easy to swap out your existing UIAlertView's with your new custom view.

You can place your custom alert view atop your topmost UIWindow to cover everything.

@interface MyAlertView : UIView
- (void)duplicateAllTheFunctionsOfUIAlertViewHere;
@end

@implementation MyAlertView : UIView
- (id)initWithFrame:(CGRectMake)frame {
  // add your custom subviews here
  // An example of a custom background color:
  self.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5];
}

- (void)duplicateAllTheFunctionsOfUIAlertViewHere {
   //re-implement all the functionality in your custom ways
}

- (void)show
{
  UIView* superview = [UIApplication sharedApplication].keyWindow;
  self.frame = superview.bounds;
  [superview addSubview:self];
  // Do an animation if you want to get fancy
}

@end

Usage in a view controller:

- (void)showAlertView
{
  UIView* alertView = [[MyAlertView alloc] initWithTextAndButtons:@"blah"];
  [alertView show];
}
like image 169
JoJo Avatar answered Apr 26 '23 04:04

JoJo