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:
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;
@end
@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];
}
@end
Or whatever you want. Notice, I set the background color of the view to be semi transparent while I have a contentView that is opaque-white that acts as the actual alert view. contentView
has a label, button, and image view as subviews. This code along with the .xib where I set up my UI with constraints, results in: