【问题标题】:How can I customize an iOS alert view?如何自定义 iOS 警报视图?
【发布时间】:2011-02-05 17:53:01
【问题描述】:

我想在我的 iOS 应用程序中创建一个自定义 alert view。比如我想在这个alert里面放一些images,并改变它的颜色。

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

【问题讨论】:

标签: iphone objective-c ios cocoa-touch


【解决方案1】:

您需要创建自己的自定义视图,并实现一些警报视图样式行为,例如模态显示、调暗背景、动画进出等。

SDK 中不支持自定义 UIAlertView,而不是文本或按钮。

【讨论】:

  • 所以我想创建自己的视图并将其显示为警报视图,对吗?谢谢您的建议...
【解决方案2】:

我设置了我自己的 UIViewController,我可以使用我自己的图像进行皮肤。我通常只使用一两个按钮,所以如果第二个按钮不被使用,我会隐藏它。视图实际上是整个屏幕的大小,所以它会阻挡它后面的触摸,但它大部分是透明的,所以背景会透出来。

在引入它时,我使用了一些动画让它像 Apple 的警报视图一样弹跳。这样的工作:

-(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。

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

【讨论】:

  • 谢谢哥们...对你的大力支持,现在让我来执行 den 回复你....再次感谢...
  • 太棒了,它看起来不像默认的反弹,但它足够相似。
【解决方案3】:

【讨论】:

    【解决方案4】:

    这是我编写的自定义警报视图,它是 UIAlertView 的直接替代品。可以设置自定义背景图片;扩展以支持自定义背景颜色并不难。

    https://github.com/TomSwift/TSAlertView

    【讨论】:

    • 我还没有提交一个应用程序还没有找到。我不使用任何私有 API。许多应用程序都有自定义的警报视图。
    【解决方案5】:

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

    如有任何困难,请告诉我。

    【讨论】:

      【解决方案6】:

      custom alert view 在 Git hub 上有很好的例子。它在核心中使用 UI 警报视图,并提供多种方法来以不同方式自定义警报视图

      【讨论】:

      • 请解释这是如何回答问题的。
      猜你喜欢
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 2014-06-10
      • 1970-01-01
      相关资源
      最近更新 更多