如何自定义iOS警报视图?

问题描述:

我想在我的iOS应用程序中创建自定义alert view。例如,我想在此alert中放置一些images,并更改其颜色。如何自定义iOS警报视图?

我知道如何创建一个正常的UIAlertView,但是有没有办法自定义一个alert view

+0

https://github.com/ankitsainisoftobiz/custom_alertview_IOS – 2016-04-08 11:03:35

+0

[此问题的迅捷版本](http://*.com/questions/25379559/custom-alert-uialertview-with-swift) – Suragch 2016-06-17 11:56:16

我建立了自己的UIViewController,我可以使用自己的图像进行皮肤处理。我通常只使用一个或两个按钮,所以我隐藏第二个按钮,如果它没有被使用。该视图实际上是整个屏幕的大小,因此它会阻挡它后面的触摸,但它大部分是透明的,所以背景显示为透明。

将它带入时,我会使用一些动画使其像苹果的警报视图一样反弹。类似这样的作品:

-(void)initialDelayEnded { 
    self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001); 
    self.view.alpha = 1.0; 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:kTransitionDuration/1.5]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(bounce1AnimationStopped)]; 
    self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1); 
    [UIView commitAnimations]; 
} 

- (void)bounce1AnimationStopped { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:kTransitionDuration/2]; 
    [UIView setAnimationDelegate:self]; 
    [UIView setAnimationDidStopSelector:@selector(bounce2AnimationStopped)]; 
    self.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9); 
    [UIView commitAnimations]; 
} 

- (void)bounce2AnimationStopped { 
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:kTransitionDuration/2]; 
    self.view.transform = CGAffineTransformIdentity; 
    [UIView commitAnimations]; 
} 

我有可能在类中建立一个短暂的延迟,所以当延迟结束时调用initialDelayEnded。

当初始化时,我传入一个对象和选择器,我想在每个按钮被按下时调用,然后在按下按钮时在对象上调用适当的选择器。

+0

感谢哥们......对于我的支持,现在让我执行den den u ....再次感谢... – 2010-04-09 04:09:08

+1

YEA,它的工作方式与我想要的一样..谢谢.. – 2010-04-09 04:54:08

+0

这很棒,它不会看起来不像默认的反弹,但它非常相似。 – 2010-12-10 03:21:31

你需要创建自己的自定义视图,并实现一些警报视图样式的行为,例如显示模式方式,调光的背景,动画和退出等

有一个在SDK中不支持定制一个比文本或按钮更远的UIAlertView。

+0

所以我想创建我自己的视图并将其显示为一个警戒视图,对吗?很好,谢谢你的建议... – 2010-04-09 04:03:44

http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html

http://www.skylarcantu.com/blog/2009/08/14/custom-uialertview-color-chooser/

您可以使用上面针对自定义提醒的链接。我希望这些会对你有所帮助。

下面是我写的一个自定义警报视图,它是UIAlertView的替代插件。您可以设置自定义背景图片;支持自定义背景颜色并不难。

https://github.com/TomSwift/TSAlertView

+0

看起来不错。这会被接受进入App Store吗? – 2011-01-24 18:39:59

+0

我还没有提交应用来确定。我不使用任何私人API。许多应用程序都有自定义的警报视图。 – TomSwift 2011-01-24 19:19:49

+0

好吧,感谢您的支持! :) – 2011-01-24 19:48:37

的UIAlertView中创建一个子类。

并为Alert View方法创建公共类。 为其添加2以下方法。现在

#pragma mark Alert View Functions 
+(void)alertViewWithYesNo:(NSString *)pstrTitle:(NSString *)pstrMessage:(int)pstrTagId:(id)pDelegate{ 
UIAlertView *objAlertNotify = [[UIAlertView alloc] init]; 
[objAlertNotify setDelegate:pDelegate]; 

[objAlertNotify addButtonWithTitle:@""]; 
[objAlertNotify addButtonWithTitle:@""]; 
int intTemp = 1; 
for (UIView* view in [objAlertNotify subviews]) 
{ 
    if ([[[view class] description] isEqualToString:@"UIAlertButton"]) 
    { 
     UILabel *theTitle = [[UILabel alloc] init]; 
     [theTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:g_AlertFontSize]]; 
     [theTitle setTextColor:[UIColor whiteColor]]; 
     switch (intTemp) { 
      case 1: 
       [theTitle setText:@"Yes"]; 
       //[theTitle setTextColor:g_ColorYes]; 
       break; 
      case 2: 
       [theTitle setText:@"No"]; 
       //[theTitle setTextColor:g_ColorNo]; 
       break; 
     } 
     intTemp++; 

     [theTitle setBackgroundColor:[UIColor clearColor]];    
     [theTitle setTextAlignment:UITextAlignmentCenter]; 
     [view addSubview:theTitle]; 
    } 
    else if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) 
    { 
     UILabel *theTitle = [[UILabel alloc] init]; 
     [theTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:g_AlertFontSize]]; 
     [theTitle setTextColor:[UIColor whiteColor]]; 
     switch (intTemp) { 
      case 1: 
       [theTitle setText:@"Yes"]; 
       //[theTitle setTextColor:g_ColorYes]; 
       break; 
      case 2: 
       [theTitle setText:@"No"]; 
       //[theTitle setTextColor:g_ColorNo]; 
       break; 
     } 
     intTemp++; 

     [theTitle setBackgroundColor:[UIColor clearColor]];    
     [theTitle setTextAlignment:UITextAlignmentCenter]; 
     [view addSubview:theTitle]; 
    } 
} 
[objAlertNotify setTag:pstrTagId]; 
[objAlertNotify setTitle:pstrTitle]; 
[objAlertNotify setMessage:pstrMessage]; 
[objAlertNotify show]; 
} 

+(void)alertViewBtnText:(UIAlertView *)alertView{ 
for (UIView* view in [alertView subviews]) 
{ 
    //NSLog(@"%@", [[view class] description]); 

    if ([[[view class] description] isEqualToString:@"UIAlertButton"]) 
    { 
     for (UILabel *lbl in [view subviews]) 
     { 
      //NSLog(@"%@", [[lbl class] description]); 

      if ([[[lbl class] description] isEqualToString:@"UILabel"]) 
      { 
       CGRect frame = [view bounds]; 

       CGSize maximumLabelSize = CGSizeMake(320,480); 
       CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode]; 
       CGRect newFrame = lbl.frame; 
       newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2; 
       newFrame.size.height = expectedLabelSize.height; 
       newFrame.size.width = expectedLabelSize.width; 
       lbl.frame = newFrame; 
       //frame.size.width = 320.0; 
       //frame.size.height = 480.0; 

       lbl.frame = frame; 
       [lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)]; 
      } 
     } 
    } 
    else if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) 
    { 
     for (UILabel *lbl in [view subviews]) 
     { 
      CGRect frame = [view bounds]; 

      CGSize maximumLabelSize = CGSizeMake(320,480); 
      CGSize expectedLabelSize = [lbl.text sizeWithFont:lbl.font constrainedToSize:maximumLabelSize lineBreakMode:lbl.lineBreakMode]; 
      CGRect newFrame = lbl.frame; 
      newFrame.origin.x = newFrame.origin.x - expectedLabelSize.width/2; 
      newFrame.size.height = expectedLabelSize.height; 
      newFrame.size.width = expectedLabelSize.width; 
      lbl.frame = newFrame; 
      //frame.size.width = 320.0; 
      //frame.size.height = 480.0; 

      lbl.frame = frame; 
      [lbl setCenter:CGPointMake([view bounds].size.width/2, [view bounds].size.height/2)]; 
     } 
    } 
} 
} 

,在任何类,您使用此自定义提醒: 以下地址:

#pragma mark UIAlertViewDelegate 
-(void)willPresentAlertView:(UIAlertView *)alertView{ 

if(alertView==objAlertMsg){ 
    /*clsCommonFuncDBAdapter *objclsCommonFuncDBAdapter = [[clsCommonFuncDBAdapter alloc] init]; 
    float newHeight = [objclsCommonFuncDBAdapter getAlertHeightByMessage:alertView.frame.size.width :alertView.message] + [g_AlertExtraHeight intValue]; 
    [objclsCommonFuncDBAdapter release]; 

    //NSLog(@"X = %f, Y = %f, Widht = %f, Height = %f", alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, alertView.frame.size.height); 
    //[alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, 110.0)]; 
    [alertView setFrame:CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y, alertView.frame.size.width, newHeight)];*/ 
} 

[clsCommonFuncDBAdapter alertViewBtnText:alertView]; 

} 

对于调用它: 使用如下:

-(void)askForGPSEnable{ 
[clsCommonFuncDBAdapter alertViewWithYesNo:msgGPSTitle :msgGPSMessage :0 :self]; 
} 

让我知道如有任何困难。

对于custom alert view,Git集线器上有很好的例子。它在核心中使用UI警报视图,并提供了多种方法来以不同方式定制警报视图

+0

请解释这是如何回答这个问题。 – ChrisF 2016-04-08 13:16:46