2010-04-08 117 views
32

我想在我的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://stackoverflow.com/questions/25379559/custom-alert-uialertview-with-swift) – Suragch 2016-06-17 11:56:16

回答

39

我建立了自己的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

2

你需要創建自己的自定義視圖,並實現一些警報視圖樣式的行爲,例如顯示模式方式,調光的背景,動畫和退出等

有一個在SDK中不支持定製一個比文本或按鈕更遠的UIAlertView。

+0

所以我想創建我自己的視圖並將其顯示爲一個警戒視圖,對嗎?很好,謝謝你的建議... – 2010-04-09 04:03:44

12

下面是我寫的一個自定義警報視圖,它是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

4

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

讓我知道如有任何困難。

1

對於custom alert view,Git集線器上有很好的例子。它在覈心中使用UI警報視圖,並提供了多種方法來以不同方式定製警報視圖

+0

請解釋這是如何回答這個問題。 – ChrisF 2016-04-08 13:16:46