码迷,mamicode.com
首页 > 其他好文 > 详细

自定义警告框(可以添加警告图片)

时间:2015-06-03 13:28:38      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:

一般我们都是使用的系统的警告框,那么如果我们要自己定义一个警告框该怎么写呢

下面我写的警告框是模仿系统写的,但是还有需要改进的地方

//写一个警告框的方法
- (void)warningWithTitle:(NSString *)title image:(NSString *)imageName storyString:(NSString *)story buttonName:(NSString *)buttonName{
    
    bigView = [[UIView alloc] initWithFrame:self.view.bounds];
    bigView.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5];
    [self.view addSubview:bigView];
    
    //警告框
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 150)];
    view.backgroundColor = [UIColor colorWithRed:250/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1];
    view.center = self.view.center;
    view.layer.cornerRadius = 8;
    view.layer.masksToBounds = YES;
    
    
    //标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = title;
    [view addSubview:titleLabel];
    
    //添加图片
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(100, 35, 50, 50)];
    image.image = [UIImage imageNamed:imageName];
    [view addSubview:image];
    
    //警告信息
    UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 150, 20)];
    secondLabel.text = story;
    secondLabel.font = [UIFont boldSystemFontOfSize:16];
    secondLabel.textAlignment = NSTextAlignmentCenter;
    [view addSubview:secondLabel];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 115, 55, 28);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(hiddonButton) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:buttonName forState:UIControlStateNormal];
    [view addSubview:button];
    [bigView addSubview:view];
    
}

- (void)hiddonButton{
    bigView.hidden = YES;
}

其中的bigView要写成全局。调用的时候,直接用self调用就可以了。当然,这只是一个小的方法,如果自己有什么额外的需求可以自己进行修改

自定义警告框(可以添加警告图片)

标签:

原文地址:http://www.cnblogs.com/nsjelly/p/4548674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!