user580175
user580175

Reputation:

Show a UIViewController as Alert View in iOS

I'm trying to create a custom Alert View similar to this

enter image description here

I want to use a View Controller as the alert because the 'X' button on the top left corner must be there, and neither a regular UIAlertController or any pod I found can help me with that.

How can I achieve this?

Thank you.

Upvotes: 0

Views: 2080

Answers (3)

Jincy Sam
Jincy Sam

Reputation: 421

try this..

@interface alertViewController ()
{
UILabel *labelTitle;
UIView *viewToShow,*viewAlert;
}

@end

@implementation alertViewController

- (void)viewDidLoad
 {
[super viewDidLoad];

viewToShow = [[UIView alloc]initWithFrame:CGRectMake(0,0, 165*2, 400)];
[viewToShow setBackgroundColor:[UIColor whiteColor]];
[viewToShow setOpaque:NO];
[self.navigationController.view addSubview:viewToShow];

labelTitle = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 165*2, 30)];
[[labelTitle layer] setCornerRadius:14];
labelTitle.text=@"TITLE";
[labelTitle setTextAlignment:NSTextAlignmentCenter];
[viewToShow addSubview:labelTitle];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
           action:@selector(cancelButtonAction:)
 forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"X" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont fontWithName:@"helvetica-neue" size:20]];
button.frame = CGRectMake(5,5,30, 40.0);
[viewToShow addSubview:button];


UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(10, 60,viewToShow.frame.size.width-20, 250)];
[textView setBackgroundColor:[UIColor lightGrayColor]];
 [viewToShow addSubview:textView];

UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 addTarget:self
           action:@selector(cancelButtonAction:)
 forControlEvents:UIControlEventTouchUpInside];
[button1 setTitle:@"Continue" forState:UIControlStateNormal];
button1.frame = CGRectMake((viewToShow.frame.size.width/2)-40,viewToShow.frame.size.height-40,80,40);
[viewToShow addSubview:button1];


[[viewToShow layer] setCornerRadius:14];

CGPoint centerForCustomExportView;


if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
    centerForCustomExportView = CGPointMake(([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.height:[[UIScreen mainScreen]bounds].size.width)/2, (([[UIScreen mainScreen]bounds].size.height>[[UIScreen mainScreen]bounds].size.width?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2));
}
else
{
    centerForCustomExportView = CGPointMake([[UIScreen mainScreen]bounds].size.width/2, ([[UIScreen mainScreen]bounds].size.height-self.navigationController.navigationBar.frame.size.height)/2);

}

viewToShow.center = centerForCustomExportView;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:)name:UIDeviceOrientationDidChangeNotification object:nil];
 }

enter image description here

Upvotes: 1

Anand Suthar
Anand Suthar

Reputation: 3798

For showing viewController as UIAlertView try this

SecondViewController *secondViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"SecondViewController"];
secondViewController.view.frame = self.view.frame;
[self.view addSubview:secondViewController.view];

secondViewController.view.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
    secondViewController.view.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished){
}];

Upvotes: 1

Nikunj
Nikunj

Reputation: 675

try this.....

- (void)show
{
    NSLog(@"show");
    isShown = YES;
    self.transform = CGAffineTransformMakeScale(0.1, 0.1);
    self.alpha = 0;
    [UIView beginAnimations:@"showAlert" context:nil];
    [UIView setAnimationDelegate:self];
    self.transform = CGAffineTransformMakeScale(1.1, 1.1);
    self.alpha = 1;
    [UIView commitAnimations];
}

Upvotes: 0

Related Questions