2010-04-08 30 views
32

Tôi muốn tạo một tùy chỉnh alert view trong ứng dụng iOS của mình. Ví dụ, tôi muốn đặt một số images trong này alert, và thay đổi màu sắc của nó.Tôi làm cách nào để tùy chỉnh chế độ xem cảnh báo trên iOS?

Tôi biết cách tạo UIAlertView bình thường, nhưng có cách nào để tùy chỉnh alert view không?

+0

https://github.com/ankitsainisoftobiz/custom_alertview_IOS –

+0

[Phiên bản Swift của câu hỏi này] (http://stackoverflow.com/question/25379559/custom-alert-uialertview-with-swift) – Suragch

Trả lời

39

Tôi thiết lập UIViewController của riêng mình mà tôi có thể da bằng hình ảnh của riêng mình. Tôi thường chỉ sử dụng một hoặc hai nút, vì vậy tôi ẩn nút thứ hai nếu nó không được sử dụng. Chế độ xem thực sự là kích thước của toàn bộ màn hình, do đó, nó chặn các điểm chạm phía sau màn hình, nhưng nó chủ yếu là trong suốt, do đó nền hiển thị qua.

Khi đưa vào, tôi sử dụng một vài hoạt ảnh để làm cho hình ảnh bị trả lại như chế độ xem cảnh báo của Apple. Một cái gì đó như thế này hoạt động:

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

Tôi có khả năng trễ ngắn được tích hợp vào lớp, do đó initialDelayEnded được gọi khi thời gian trễ kết thúc.

Khi khởi tạo, tôi chuyển vào đối tượng và bộ chọn Tôi muốn gọi khi nhấn từng nút và sau đó tôi gọi bộ chọn thích hợp trên đối tượng khi nhấn nút.

+0

Cảm ơn bạn ... vì đã hỗ trợ tốt đẹp cho ur, bây giờ hãy để tôi thực hiện trả lời u .... cảm ơn lần nữa ... –

+1

YEA, nó hoạt động theo cách tương tự như tôi muốn .. cảm ơn .. –

+0

Thật tuyệt, nó không 't trông chính xác như các thư bị trả lại mặc định nhưng nó hoàn toàn tương tự. –

2

Bạn sẽ cần phải tạo ra giao diện tùy chỉnh của riêng bạn, và thực hiện một số hành vi xem phong cách cảnh báo, chẳng hạn như hiển thị modally, mờ nền, hiệu ứng động trong và ngoài vv

Không có hỗ trợ trong SDK cho tùy chỉnh UIAlertView hơn bất kỳ văn bản hoặc nút nào.

+0

Vì vậy, tôi giả sử để tạo ra quan điểm của riêng tôi và den hiển thị nó như thể nó là một cái nhìn cảnh báo, phải không ??? cảm ơn tốt cho đề nghị ur ... –

12

Đây là chế độ xem cảnh báo tùy chỉnh mà tôi đã viết là bản thay thế thả xuống cho UIAlertView. Bạn có thể đặt hình nền tùy chỉnh; nó sẽ không khó để mở rộng để hỗ trợ màu nền tùy chỉnh.

https://github.com/TomSwift/TSAlertView

+0

Có vẻ tốt. Điều đó có được chấp nhận vào App Store không? –

+0

Tôi chưa gửi ứng dụng để tìm hiểu. Tôi không sử dụng bất kỳ API riêng tư nào. Rất nhiều ứng dụng đã tùy chỉnh chế độ xem cảnh báo. – TomSwift

+0

Vâng, cảm ơn vì đã tạo ra nó! :) –

4

Tạo một lớp con cho UIAlertView.

Và tạo lớp học chung cho phương thức Chế độ xem thông báo. Thêm 2 phương thức dưới đây cho nó.

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

Bây giờ, trong bất cứ lớp học, bạn đang sử dụng Alert tùy chỉnh này: Thêm bên dưới:

#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]; 

} 

Đối gọi đó là: Sử dụng như dưới đây:

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

Hãy cho tôi biết trong trường hợp có bất kỳ khó khăn nào.

1

Có ví dụ rất tốt về trung tâm Git cho custom alert view.Nó đang sử dụng chế độ xem cảnh báo giao diện người dùng trong lõi và cung cấp số phương pháp để tùy chỉnh chế độ xem cảnh báo theo các cách khác nhau

+0

Vui lòng giải thích cách này trả lời câu hỏi. – ChrisF

Các vấn đề liên quan