首頁>Program>source

我想建立一个自定義的 alert view 在我的iOS應用程式中.例如,我想放一些 images 在這 alert ,並更改其颜色。

我知道如何建立普通的 UIAlertView ,但是有一種方法可以自定義 alert view

最新回復
  • 5月前
    1 #

    我設置了自己的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。

    初始化時,我傳入一个物件和要在按下每个按钮時呼叫的選擇器,然後在按下按钮時在该物件上呼叫適当的選擇器。

  • 5月前
    2 #

    這是自定義警報 我寫的视圖是UIAlertView的直接替代.您可以設置自定義背景圖片; 擴充套件支援自定義背景颜色將很容易。

    https://github.com/TomSwift/TSAlertView

  • 5月前
    3 #

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

    万一遇到困难,請告诉我。

  • 5月前
    4 #

    您需要建立自己的自定義视圖,並實現一些警報视圖樣式的行為,例如以模態顯示,調暗背景,動画顯示 等等。

    除了文字或按钮之外,SDK中不支援自定義UIAlertView。

  • 5月前
    5 #

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

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

    您可以使用以上鏈接来定製警報.希望這些對您有所帮助。

  • android:為具有不同專案佈局的ListView建立ViewHolders
  • 如何使用目標C在iOS本地下載和儲存檔案?