ios newbie ios newbie - 6 months ago 18
Objective-C Question

Present small ModalVIewController ObjectiveC

My task is adding new simple notification modal view in some existing VC's.
At best, I want to implement a function that shows notificationVC over current view.

There are a lot of similar questions, but they are not working for me or needs delegates, segues, etc.

Thanks, this is like what I want:

enter image description here


If you want to present a view controller over another view controller, it is easiest to set the modalPresentationStyle to either UIModalPresentationOverFullScreen or UIModalPresentationOverCurrentContext. If the presented view controller's view has a background color with alpha less than one the presenting view controller's view will show through. Using your example, in your presenting view controller you can say something like:

- (void) presentNotificationViewController
    NotificationViewController *notificationVC = [[NotificationViewController alloc] init];
    notificationVC.titleText = @"My Notification Title";
    notificationVC.image = image;
    notificationVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    notificationVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:notificationVC animated:YES completion:nil];

Then your NotificationViewController.m can be something like:

#import "NotificationViewController.h"

@interface NotificationViewController ()
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UILabel *notificationTitleLabel;
@property (weak, nonatomic) IBOutlet UIButton *dismissButton;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@implementation NotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.60];

    self.contentView.layer.cornerRadius = 3.0;

    self.notificationTitleLabel.text = self.titleText;

    self.imageView.image = self.image;

    self.dismissButton.layer.cornerRadius = 3.0;
    self.dismissButton.layer.borderColor = [UIColor blackColor].CGColor;
    self.dismissButton.layer.borderWidth = 2.0;
    // etc.


- (IBAction)dismissPressed:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];


Or whatever you want. Results in:

enter image description here