天天看點

iOS 自定義UIAlertView

系統的UIAlertView(iOS9廢棄變為UIAlertController)雖然簡潔友善,但是有些環境不适用。

比如遊戲啊,動漫類APP和戀愛類APP。都需要一些萌系的或者好看得UIAlertView。

這時候我們該怎麼辦呢?

這裡有一個小tip教給大家。

先到百度圖檔 搜尋“背景框素材” 會出現一大堆的圖檔素材供你使用。

我選擇了一張 經過處理之後變成後面這種樣子。

iOS 自定義UIAlertView
iOS 自定義UIAlertView

找好素材之後完事具備 隻欠東風了

我們的思路是 建一個UIWindow 然後再window上加UIView UIImageView UILabel UIButton等控件

上代碼

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) UIWindow *window;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeRoundedRect)];
    btn.frame = CGRectMake(100, 100, 100, 100);
    [btn setTitle:@"點我" forState:(UIControlStateNormal)];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(TanchuAction) forControlEvents:(UIControlEventTouchUpInside)];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)TanchuAction
{
           
//建立一個UIWindow
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [_window makeKeyAndVisible];

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 50, self.view.frame.size.width/2)];
    view.center = _window.center;
    view.backgroundColor = [UIColor clearColor];
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;
    [_window addSubview:view];
    
    UIImageView *imgView = [[UIImageView alloc]initWithFrame:view.bounds];
    imgView.image = [UIImage imageNamed:@"1"];
    imgView.userInteractionEnabled = YES;
    [view addSubview:imgView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, imgView.frame.size.width-10, 30)];
    label.text = @"溫馨提示";
    label.textAlignment = NSTextAlignmentCenter;
    [imgView addSubview:label];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(60, 50, imgView.frame.size.width-150, 90)];
    label1.text = @"  确定要退出遊戲嗎?(退出後遊戲進度不予儲存)";
    label1.numberOfLines = 0;
    [imgView addSubview:label1];
    
    UIButton *btn1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn1.frame = CGRectMake(60, 140, 60, 30);
    [btn1 setTitle:@"确定" forState:(UIControlStateNormal)];
    [imgView addSubview:btn1];
    [btn1 addTarget:self action:@selector(queding) forControlEvents:(UIControlEventTouchUpInside)];
    btn1.backgroundColor = [UIColor brownColor];
    btn1.titleLabel.tintColor = [UIColor whiteColor];
    
    
    UIButton *btn2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn2.frame = CGRectMake(imgView.frame.size.width-160, 140, 60, 30);
    [btn2 setTitle:@"取消" forState:(UIControlStateNormal)];
    [imgView addSubview:btn2];
    [btn2 addTarget:self action:@selector(qvxiao) forControlEvents:(UIControlEventTouchUpInside)];
    btn2.backgroundColor = [UIColor brownColor];
    btn2.titleLabel.tintColor = [UIColor whiteColor];
    
    
    [self.view addSubview:_window];
}
- (void)queding
{
    NSLog(@"确定");
    self.window.hidden = YES;
}
- (void)qvxiao
{
    NSLog(@"取消");
    self.window.hidden = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
           

我這個隻是為測試,并未做适配(在6s上顯示效果)

效果如下

iOS 自定義UIAlertView

這個不僅可以做UIAlertView 還可以做很多東西 比如說自定義的UIActivityIndicatorView

隻有你想不到的,沒有它做不到的~

畢竟是自己寫的,可能會有錯誤,還請讀者指正,謝謝。