• 自定义UIAlertView


     

    终于到了周末了,可以抽点时间写自己的第一篇原创文章了,这是本人第一次写文章,请大家多捧场,写得不好请见谅,当然有任何问题也可以给我留言,非常乐意和大家交流学习。

    支持原创,转载请注明出处。

    其实这个例子是从网上找到的,自己研究了一下,终于搞明白了如何自定义UIAlertView,将其修改之后为大家分享。

    CustomAlertView.h
    @interface CustomAlertView : UIAlertView
    
    @end
    @implementation CustomAlertView
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    -(void)layoutSubviews{//这个是重点!!重新定义UIAlertView中的各种控件,包括UILabel,UIButton等
        for (UIView *v in self.subviews) {//遍历UIAlertView中的所有控件(UIView),再将它们重新设置
            if ([v isKindOfClass:[UILabel class]]) {//设置Label
                UILabel *label = (UILabel *)v;
                if ([label.text isEqualToString:self.title]) {
                    label.font = [UIFont boldSystemFontOfSize:40];
                    label.numberOfLines = 0;
                    label.lineBreakMode = UILineBreakModeWordWrap;
                    label.textColor = [UIColor redColor];
                    label.backgroundColor = [UIColor clearColor];
                    label.textAlignment = UITextAlignmentCenter;
                    label.shadowColor = [UIColor blackColor];
                    label.shadowOffset = CGSizeMake(0, -1);//设置阴影
                    [label sizeToFit];//文字大小自动适应
                    label.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.2);
                }else{
                    label.font = [UIFont boldSystemFontOfSize:20];
                    label.numberOfLines = 0;
                    label.lineBreakMode = UILineBreakModeWordWrap;
                    label.textColor = [UIColor yellowColor];
                    label.backgroundColor = [UIColor clearColor];
                    label.textAlignment = UITextAlignmentCenter;
                    label.shadowColor = [UIColor blackColor];
                    label.shadowOffset = CGSizeMake(0, -1);
                    label.center = CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.3);
                }
            }
            if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")]) {//设置Button
                UIButton *button = (UIButton *)v;
                UIImage *image = nil;
                if (button.tag == 1) {//给button的标签赋一个值,以便区分左右两个Button,对其进行更详细的设置。
                    image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"red"]];
                }else{
                    image = [UIImage imageNamed:[NSString stringWithFormat:@"alert-%@-button.png", @"black"]];
                }
                image = [image stretchableImageWithLeftCapWidth:(int)(image.size.width+1)>>1 topCapHeight:0];//这句话的作用是:将image图片的内容拉伸,而边角不拉伸。>>是右移运算符,感兴趣的同学可以去百度一下,这里不需要知道原理,了解作用就行了
                button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
                button.titleLabel.minimumFontSize = 10;
                button.titleLabel.textAlignment = UITextAlignmentCenter;
                button.titleLabel.shadowOffset = CGSizeMake(0, -1);
                button.backgroundColor = [UIColor clearColor];
                [button setBackgroundImage:image forState:UIControlStateNormal];
                if (button.tag == 1)
                button.center = CGPointMake(self.bounds.size.width*0.25, self.bounds.size.height*0.75);
                else
                    button.center = CGPointMake(self.bounds.size.width*0.75, self.bounds.size.height*0.5);
    //            [button setTitleColor:kAlertViewButtonTextColor forState:UIControlStateNormal];
                [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }
    }
    
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
        UIImageView *pic = [[UIImageView alloc]initWithFrame:CGRectMake(230, 45, 60, 60)];
        [pic setImage:[UIImage imageNamed:@"LittleWhitePig.png"]];
        [self addSubview:pic];
        [pic release];
    }
    
    - (void)show
    {
        [super show];
        self.bounds = CGRectMake(0, 100, 320, 300);//更改整个UIAlertView的框架
    }
    
    @end

    代码很简单,重点-(void)layoutSubviews方法,这个方法是设置系统的控件的,设置思想就是先遍历所有的空间,再判断这是什么控件,if ([v isKindOfClass:NSClassFromString(@"UIAlertButton")])这个可以判断是不是UIAlertButton的子类。

    接下来就只需要在ViewController里面创建这个类的对象就行了,再show就行了

    - (IBAction)showAlertView:(id)sender
    {
         CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"" message:@"白猪" delegate:nil cancelButtonTitle:@"" otherButtonTitles:@"很帅", nil];
        [alert show];
      

    然后附上效果图:

    有什么问题请留言,欢迎指正

  • 相关阅读:
    CSS之旅——第二站 如何更深入的理解各种选择器
    CSS之旅——第一站 为什么要用CSS
    记录一些在用wcf的过程中走过的泥巴路 【第一篇】
    asp.net mvc 之旅—— 第二站 窥探Controller下的各种Result
    asp.net mvc 之旅—— 第一站 从简单的razor入手
    Sql Server之旅——终点站 nolock引发的三级事件的一些思考
    Sql Server之旅——第十四站 深入的探讨锁机制
    Sql Server之旅——第十三站 对锁的初步认识
    Sql Server之旅——第十二站 sqltext的参数化处理
    Sql Server之旅——第十一站 简单说说sqlserver的执行计划
  • 原文地址:https://www.cnblogs.com/xiaobaizhu/p/UIAlertView.html
Copyright © 2020-2023  润新知