2010-04-08 17 views
32

मैं अपने आईओएस एप्लिकेशन के भीतर एक कस्टम alert view बनाना चाहता हूं। उदाहरण के लिए, मैं इस alert में कुछ images डालना चाहता हूं, और इसका रंग बदलना चाहता हूं।मैं आईओएस अलर्ट व्यू को कैसे अनुकूलित कर सकता हूं?

मुझे पता है कि सामान्य UIAlertView कैसे बनाएं, लेकिन alert view को अनुकूलित करने का कोई तरीका है?

+0

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

+0

[इस प्रश्न का स्विफ्ट संस्करण] (http://stackoverflow.com/प्रश्न/25379559/कस्टम-अलर्ट- uialertview-with-swift) – Suragch

उत्तर

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

धन्यवाद दोस्त ... आपके अच्छे समर्थन के लिए, अब मुझे डेन जवाब लागू करने दें .... धन्यवाद ... –

+1

वाईईए, यह वैसे ही काम कर रहा है जैसा मैं चाहता था .. धन्यवाद .. –

+0

यह बहुत अच्छा है, यह नहीं करता बिल्कुल डिफ़ॉल्ट बाउंस की तरह दिखता नहीं है लेकिन यह पर्याप्त रूप से समान है। –

2

आप अपने स्वयं के कस्टम दृश्य बनाते हैं, और इस तरह के रीति से, आदि प्रदर्शित पृष्ठभूमि मंद, अंदर और बाहर एनिमेट, के रूप में, कुछ चेतावनी दृश्य शैली व्यवहार को लागू करने की आवश्यकता होगी

के लिए SDK में कोई समर्थन नहीं है टेक्स्ट या बटन से आगे UIAlertView को अनुकूलित करना।

+0

तो मुझे लगता है कि मेरा खुद का दृश्य बनाना और डेन इसे प्रदर्शित करना है जैसे कि यह एक चेतावनी दृश्य था, सही ??? आपके सुझाव के लिए अच्छा धन्यवाद ... –

12

यहां एक कस्टम अलर्ट व्यू है जिसे मैंने लिखा है जो UIAlertView के लिए ड्रॉप-इन प्रतिस्थापन है। आप कस्टम पृष्ठभूमि छवि सेट कर सकते हैं; कस्टम पृष्ठभूमि रंगों का समर्थन करने के लिए विस्तार करना मुश्किल नहीं होगा।

https://github.com/TomSwift/TSAlertView

+0

अच्छा लग रहा है। क्या इसे ऐप स्टोर में स्वीकार किया जाएगा? –

+0

मैंने अभी तक पता लगाने के लिए एक ऐप सबमिट नहीं किया है। मैं किसी भी निजी एपीआई का उपयोग नहीं करता हूं। बहुत सारे ऐप्स ने अलर्ट दृश्य अनुकूलित किए हैं। – TomSwift

+0

अच्छा, इसे बनाने के लिए धन्यवाद! :) –

4

UIAlertView के लिए एक उप वर्ग बनाएँ।

और अलर्ट व्यू विधि के लिए सामान्य श्रेणी बनाएं। इसके लिए 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 के लिए गिट हब पर बहुत अच्छा उदाहरण है।यह कोर में यूआई अलर्ट व्यू का उपयोग कर रहा है और विभिन्न तरीकों से अलर्ट व्यू को कस्टमाइज़ करने के तरीकों की संख्या प्रदान करता है

+0

कृपया बताएं कि यह प्रश्न का उत्तर कैसे देता है। – ChrisF

संबंधित मुद्दे

 संबंधित मुद्दे