Search code examples
iosobjective-cios6uinavigationcontrolleralert

UInavigationcontroller Global Alert Message (like WhatsApp incoming message)


Am building an Application (Forum) and I have an UINavigationController. I want to display an alert message in whatever UIViewController the user is. I have really no idea how i should do it.

Thx a lot for your help.

Here some examples of what i want to do: and here is the sample screenshot


Solution

  • You can add this view to the keyWindow, and bring it to the front. Or you can make your alertView as a UIWindow, by this way, you can show it anywhere.

    static UIWindow *_sharedNavigationBarAlertView = nil;
    + (UIWindow *)sharedNavigationBarAlertView
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _sharedNavigationBarAlertView = [[UIWindow alloc] initWithFrame:CGRectZero];
            _sharedNavigationBarAlertView.windowLevel = UIWindowLevelStatusBar + 1.0;
            _sharedNavigationBarAlertView.hidden = YES;
            // add other views...
        });
        return _sharedNavigationBarAlertView;
    }
    
    + (void)showWithInformation:(id)info
    {
        // [self sharedNavigationBarAlertView].imageView.image = ...;
        // [self sharedNavigationBarAlertView].titleLabel.text = @"";
        // [self sharedNavigationBarAlertView].detailLabel.text = @"";
        CGRect frame = [UIScreen mainScreen].bounds;
        frame.size.height = 44.0;
        [self sharedNavigationBarAlertView].frame = frame;
        [self sharedNavigationBarAlertView].hidden = NO;
    }
    + (void)hide
    {
        [self sharedNavigationBarAlertView].hidden = YES;
    }
    
    // To release the shared alert window, just set it to nil