在iOS中将UIViewController显示为警报视图

时间:2015-08-11 10:45:59

标签: ios objective-c iphone

我正在尝试创建类似于此

的自定义警报视图

enter image description here

我想使用View Controller作为警报,因为左上角的'X'按钮必须在那里,并且我找到的常规UIAlertController或任何pod都不能帮助我。

我怎样才能做到这一点?

谢谢。

3 个答案:

答案 0 :(得分:1)

要将viewController显示为UIAlertView,请尝试使用

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){
}];

答案 1 :(得分:1)

试试这个..

@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

答案 2 :(得分:0)

试试这个......

- (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];
}