iOS 사용자 정의 alertView 알림 상자 인 스 턴 스 공유

본 사례 는 iOS 사용자 정의 alertView 알림 상 자 를 공유 합 니 다.먼저 그림,탄 상자 의 배경 색,단추 배경 색,알림 메시지 의 글꼴 색 을 바 꿀 수 있 습 니 다.

단일 사례 를 이용 하여 풍부 한 사용자 정의 인 터 페 이 스 를 실현 하 다.

//
// PBAlertController.h
// PBAlertDemo
//
// Created by     on 16/4/20.
// Copyright © 2016     . All rights reserved.
//

#import <UIKit/UIKit.h>


typedef void(^PBBlock)();

@interface PBAlertController : UIViewController


/**   alertView    */
@property (nonatomic, copy) UIColor *alertBackgroundColor;
/**           */
@property (nonatomic, copy) UIColor *btnConfirmBackgroundColor;
/**           */
@property (nonatomic, copy) UIColor *btnCancelBackgroundColor;
/**   message     */
@property (nonatomic, copy) UIColor *messageColor;

/**      */
+(instancetype)shareAlertController;
/**   alertView         block */
-(void)alertViewControllerWithMessage:(NSString *)message andBlock:(PBBlock) block;

@end

.m 파일 에서 컨트롤 을 초기 화하 고 alertView 컨트롤 의 속성 을 게 으 름 피 워 서 초기 색상 을 확인 합 니 다.

//
// PBAlertController.m
// PBAlertDemo
//
// Created by     on 16/4/20.
// Copyright © 2016     . All rights reserved.
//

#import "PBAlertController.h"

/**      */
#define kMainScreenBounds [UIScreen mainScreen].bounds

@interface PBAlertController ()

/**    */
@property (nonatomic, strong) UIView *coverView;
/**    */
@property (nonatomic, strong) UIView *alertView;
/**        block */
@property (nonatomic, copy) PBBlock block;

@end

@implementation PBAlertController

- (void)viewDidLoad {

 [super viewDidLoad];
 self.view.backgroundColor = [UIColor whiteColor];
}

-(void)alertViewControllerWithMessage:(NSString *)message andBlock:(PBBlock) block{

 self.block = block;
 //    
 UIView * coverView = [[UIView alloc] initWithFrame:kMainScreenBounds];
 self.coverView = coverView;
 [self.view addSubview:coverView];
 coverView.backgroundColor = [UIColor blackColor];
 coverView.alpha = 0.7;
 
 //     view
 UIView * alertView = [[UIView alloc] init];
 alertView.backgroundColor = self.alertBackgroundColor;
 //      
 alertView.layer.cornerRadius = 6.0;
 self.alertView = alertView;
 [self.view addSubview:alertView];
 alertView.center = coverView.center;
 alertView.bounds = CGRectMake(0, 0, kMainScreenBounds.size.width * 0.75, kMainScreenBounds.size.width * 0.75 * 1.5/ 3);
 
 //       label
 UILabel * label = [[UILabel alloc] init];
 [alertView addSubview:label];
 label.text = @"    ";
 label.font = [UIFont systemFontOfSize:19];
 label.textAlignment = NSTextAlignmentCenter;
 CGFloat lblWidth = alertView.bounds.size.width;
 CGFloat lblHigth = 22;
 label.frame = CGRectMake(0, 0, lblWidth, lblHigth);
 
 //         
 UIView * separateLine = [[UIView alloc] init];
 separateLine.backgroundColor = [UIColor grayColor];
 [alertView addSubview:separateLine];
 separateLine.frame = CGRectMake(0, lblHigth + 1, alertView.bounds.size.width, 1);
 
 //  message label
 UILabel * lblMessage = [[UILabel alloc] init];
 lblMessage.textColor = self.messageColor;
 [alertView addSubview:lblMessage];
 lblMessage.text = message;
 lblMessage.textAlignment = NSTextAlignmentCenter;
 lblMessage.numberOfLines = 2; //      Message
 CGFloat margin = 5;
 CGFloat msgX = margin;
 CGFloat msgY = lblHigth + 3;
 CGFloat msgW = alertView.bounds.size.width - 2 * margin;
 CGFloat msgH = 44;
 lblMessage.frame = CGRectMake(msgX, msgY, msgW, msgH);
 
 //         
 CGFloat buttonWidth = (alertView.bounds.size.width - 4 * margin) * 0.5;
 CGFloat buttonHigth = 25;
 UIButton * btnCancel = [[UIButton alloc] init];
 [alertView addSubview:btnCancel];
 [btnCancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 [btnCancel setTitle:@"  " forState:UIControlStateNormal];
 [btnCancel setBackgroundColor:self.btnCancelBackgroundColor];
 btnCancel.frame = CGRectMake(margin, alertView.bounds.size.height - margin - buttonHigth, buttonWidth, buttonHigth);
 btnCancel.tag = 0;
 [btnCancel addTarget:self action:@selector(didClickBtnConfirm:) forControlEvents:UIControlEventTouchUpInside];
 //    
 UIButton * btnConfirm = [[UIButton alloc] init];
 btnConfirm.tag = 1;
 [alertView addSubview:btnConfirm];
 [btnConfirm setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
 [btnConfirm setTitle:@"  " forState:UIControlStateNormal];
 [btnConfirm setBackgroundColor:self.btnConfirmBackgroundColor];
 btnConfirm.frame = CGRectMake(alertView.bounds.size.width - margin - buttonWidth, alertView.bounds.size.height - margin - buttonHigth, buttonWidth, buttonHigth);
 [btnConfirm addTarget:self action:@selector(didClickBtnConfirm:) forControlEvents:UIControlEventTouchUpInside];

}

/**      or        */
-(void)didClickBtnConfirm:(UIButton *)sender{

 if (sender.tag == 0) {
  [self dismissViewControllerAnimated:YES completion:nil];
  return;
 }
 self.block();
 [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
 [super didReceiveMemoryWarning];
}

static PBAlertController * instance = nil;
+(instancetype)shareAlertController{

 static dispatch_once_t onceToken;
 dispatch_once(&onceToken, ^{
  instance = [[PBAlertController alloc] init];
 });
 return instance;
}

-(UIColor *)alertBackgroundColor{

 if (_alertBackgroundColor == nil) {
  _alertBackgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
 }
 return _alertBackgroundColor;
}

/**         */
-(UIColor *)btnConfirmBackgroundColor{

 if (_btnConfirmBackgroundColor == nil) {
  _btnConfirmBackgroundColor = [UIColor orangeColor];
 }
 return _btnConfirmBackgroundColor;
}

/**         */
-(UIColor *)btnCancelBackgroundColor{

 if (_btnCancelBackgroundColor == nil) {
  _btnCancelBackgroundColor = [UIColor blueColor];
 }
 return _btnCancelBackgroundColor;
}

/** message     */
-(UIColor *)messageColor{

 if (_messageColor == nil) {
  _messageColor = [UIColor blackColor];
 }
 return _messageColor;
}
@end

호출 이 필요 한 곳 에서 호출 하 다

//
// ViewController.m
// PBAlertDemo
//
// Created by     on 16/4/20.
// Copyright © 2016     . All rights reserved.
//

#import "ViewController.h"
#import "PBAlertController.h"
@interface ViewController ()

@end

@implementation ViewController

//         
- (IBAction)clickShowAlertBtn:(id)sender {
 
 PBAlertController * alertVc = [PBAlertController shareAlertController];
 alertVc.messageColor = [UIColor redColor];
 [alertVc alertViewControllerWithMessage:@"   message  " andBlock:^{
  NSLog(@"          ");
 }];
 alertVc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
 [self presentModalViewController:alertVc animated:YES];
}

@end
이상 은 본 고의 모든 내용 입 니 다.iOS 프로 그래 밍 을 배 우 는 데 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기