前言
公司需要的输入框
公司需要的提示
提示可以使用UIAlertViewController 这里做了封装 为了使UI更好看
思路
我第一反应是使用view展示,但明显使用controller更好
present之前有个枚举modalPresentationStyle 这里选择UIModalPresentationOverFullScreen 可满足我们的需要
使用xib后的效果如下
可以看到中间是一个view ,这是为了方便以后扩充
实现
1>定义一个新的NSObject DHCustomAlertControllerMaker 里面保存界面的一些信息
如标题,按钮的颜色等
2>.h中的代码
+ (instancetype)controllerCustomAlert:(void (^)(DHCustomAlertControllerMaker *maker))make;
@property (nonatomic, strong) DHCustomAlertControllerMaker *maker;
/// 点击功能按钮回调 未处理则dismissViewController
@property (nonatomic, copy, nullable) void (^ didOnclickActionItemCallback)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent);
/// 点击取消按钮回调 未处理则dismissViewController
@property (nonatomic, copy, nullable) void (^ didOnclickCancelItemCallback)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent);
/// 点击遮罩视图回调
@property (nonatomic, copy, nullable) void (^ didOnclickMaskViewItemCallback)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent);
- (void)showInController:(UIViewController *)controller;
- (void)dismissViewController;
.h中的代码有以下几部分组成
1.使用类方法加载一个方法controllerCustomAlert:
这里是使用block回调,使用时直接使用回调加载一些信息
2.定义DHCustomAlertControllerMaker对象
3.定义需要的block回调
4.声明两个方法 跳转 和 返回
3>.m中的代码
+ (instancetype)controllerCustomAlert:(void (^)(DHCustomAlertControllerMaker *maker))make
{
DHCustomAlertController *controller = [self controllerFormXib];
controller.maker = [DHCustomAlertControllerMaker new];
if (make) {
make(controller.maker);
}
return controller;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self initializationData];
[self initializationView];
}
- (void)initializationData
{
}
- (void)initializationView
{
self.contentView.backgroundColor = UIColor.clearColor;
self.view.backgroundColor = UIColor.clearColor;
self.lblTitle.text = self.maker.alertTitle;
if (self.maker.alertContentView) {
[self.contentView addSubview:self.maker.alertContentView];
[self.maker.alertContentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.contentView);
}];
self.contentViewHeight.active = NO;
}
if (self.maker.cancelBtnTitle) {
self.cancelBtn.hidden = NO;
[self.cancelBtn setTitle:self.maker.cancelBtnTitle forState:UIControlStateNormal];
} else {
self.cancelBtn.hidden = YES;
}
if (self.maker.actionBtnTitle) {
self.actionBtn.hidden = NO;
[self.actionBtn setTitle:self.maker.actionBtnTitle forState:UIControlStateNormal];
} else {
self.actionBtn.hidden = YES;
}
[self.cancelBtn setTitleColor:self.maker.cancelBtnTitleColor
forState:UIControlStateNormal];
[self.actionBtn setTitleColor:self.maker.actionBtnTitleColor
forState:UIControlStateNormal];
[self.view layoutIfNeeded];
// if ([self.maker.alertContentView performSelector:@selector(becomeFirstResponder)]) {
// [self.maker.alertContentView becomeFirstResponder];
// }
}
#pragma mark - Action
- (IBAction)onclickMaskViewItem:(id)sender
{
if ([self.maker.alertContentView performSelector:@selector(isFirstResponder)]) {
if (self.maker.alertContentView.isFirstResponder) {
[self.view endEditing:YES];
return;
}
}
[self.view endEditing:YES];
if (self.didOnclickMaskViewItemCallback) {
self.didOnclickMaskViewItemCallback(self, self.maker.alertContentView.contentDict);
}
}
- (IBAction)onclickCancelItem:(id)sender
{
[self.view endEditing:YES];
if (self.didOnclickCancelItemCallback) {
self.didOnclickCancelItemCallback(self, self.maker.alertContentView.contentDict);
} else {
[self dismissViewController];
}
}
- (IBAction)onclickActionItem:(id)sender
{
[self.view endEditing:YES];
if ([self.maker.alertContentView respondsToSelector:@selector(verificationContent)]) {
NSError *error = [self.maker.alertContentView verificationContent];
if (error) {
[self mc_showCenterMessage:error.localizedDescription];
return;
}
}
if (self.didOnclickActionItemCallback) {
if ([self.maker.alertContentView respondsToSelector:@selector(contentDict)]) {
self.didOnclickActionItemCallback(self, self.maker.alertContentView.contentDict);
} else {
self.didOnclickActionItemCallback(self, nil);
}
} else {
[self dismissViewController];
}
}
#pragma mark - Public
- (void)showInController:(UIViewController *)controller
{
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
[controller presentViewController:self animated:NO completion:nil];
}
- (void)dismissViewController
{
[self dismissViewControllerAnimated:NO completion:nil];
}
1.实现controllerCustomAlert:方法,重点是调用block之前创建一个新的DHCustomAlertControllerMaker对象
2.将alertContentView加载到中的contentView上,带下和contentView的大小相同
3.实现两个方法 present 和 dismiss
这里直接使用UIModalPresentationOverFullScreen方法 效果更好
4>定义一个DHAlertTextContentView控件
DHAlertTextContentView主要是中间的view 这里就是UITextView 直接上代码
+ (instancetype)alertTextContentViewWithText:(NSString *)contentText
contentPlaceHolder:(NSString *)placeHolder;
#import "DHAlertTextContentView.h"
#import "UITextView+JKPlaceHolder.h"
@interface DHAlertTextContentView ()
@property (strong, nonatomic) UITextView *txtContent;
@end
@implementation DHAlertTextContentView
- (instancetype)init
{
self = [super init];
if (self) {
[self initializationData];
[self initializationViews];
}
return self;
}
+ (instancetype)alertTextContentViewWithText:(NSString *)contentText
contentPlaceHolder:(NSString *)placeHolder
{
DHAlertTextContentView *contentView = [[self alloc] init];
contentView.txtContent.text = contentText;
[contentView.txtContent jk_addPlaceHolder:placeHolder];
return contentView;
}
#pragma mark – setup Data
- (void)initializationData
{
}
#pragma mark – loadViews
- (void)initializationViews
{
self.txtContent.jk_placeHolderTextView.font = self.txtContent.font;
self.txtContent.jk_placeHolderTextView.textColor = kUIColorFromRGB(0xBFBFBF);
[self addSubview:self.txtContent];
[self.txtContent mas_makeConstraints:^(MASConstraintMaker *make) {
make.topMargin.mas_equalTo(0);
make.leadingMargin.mas_equalTo(4);
make.trailingMargin.mas_equalTo(-4);
make.bottomMargin.mas_equalTo(0);
make.height.mas_equalTo(65);
}];
}
#pragma mark - Getter
- (UITextView *)txtContent
{
if (_txtContent == nil) {
_txtContent = [[UITextView alloc] init];
_txtContent.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
_txtContent.cornerRadiu = 9;
_txtContent.borderColor = kUIColorFromRGB(0xB2B2B2);
_txtContent.borderWidth = 0.5f;
}
return _txtContent;
}
#pragma mark - DHCostomAlertContentViewDelegate
- (NSError *)verificationContent
{
if (self.txtContent.text.length == 0) {
return [NSError errorWithDomain:NSURLErrorDomain
code:0
userInfo:@{
NSLocalizedDescriptionKey : @"请输入内容"
}];
}
return nil;
}
- (NSDictionary *)contentDict
{
return @{
kDHAlertTextContentViewContentKey : self.txtContent.text ?: @"",
};
}
- (BOOL)isFirstResponder
{
return self.txtContent.isFirstResponder;
}
- (void)becomeFirstResponder
{
[self.txtContent becomeFirstResponder];
}
@end
5>定义分类
@interface DHCustomAlertController (Factory)
#pragma mark - 多行输入
+ (instancetype)showAlertTextControllerWithController:(UIViewController *)formController
title:(NSString *)title
content:(NSString * __nullable)content
contentPlaceHolder:(NSString * __nullable)contentPlaceHolder
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *dicContent))callback;
+ (instancetype)showAlertTextControllerWithController:(UIViewController *)formController
title:(NSString *)title
content:(NSString * __nullable)content
contentPlaceHolder:(NSString * __nullable)contentPlaceHolder
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
actionButtonTitleColor:(UIColor * __nullable)actionButtonTitleColor
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *dicContent))callback;
#pragma mark - 文字提示
+ (instancetype)showAlertControllerWithController:(UIViewController *)formController
title:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent))callback;
+ (instancetype)showAlertControllerWithController:(UIViewController *)formController
title:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
actionButtonTitleColor:(UIColor * __nullable)actionButtonTitleColor
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent))callback;
@end
@implementation DHCustomAlertController (Factory)
+ (instancetype)showAlertTextControllerWithController:(UIViewController *)formController
title:(NSString *)title
content:(NSString * __nullable)content
contentPlaceHolder:(NSString * __nullable)contentPlaceHolder
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *dicContent))callback
{
return [self showAlertTextControllerWithController:formController
title:title
content:content
contentPlaceHolder:contentPlaceHolder
cancelButtonTitle:cancelButtonTitle
actionButtonTitle:actionButtonTitle
actionButtonTitleColor:nil
onclickActionItemCallback:callback];
}
+ (instancetype)showAlertTextControllerWithController:(UIViewController *)formController
title:(NSString *)title
content:(NSString * __nullable)content
contentPlaceHolder:(NSString * __nullable)contentPlaceHolder
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
actionButtonTitleColor:(UIColor * __nullable)actionButtonTitleColor
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *dicContent))callback
{
DHCustomAlertController *controller = [DHCustomAlertController controllerCustomAlert:^(DHCustomAlertControllerMaker * _Nonnull maker) {
maker.alertTitleSet(title);
maker.cancelBtnTitleSet(cancelButtonTitle);
maker.actionBtnTitleSet(actionButtonTitle).actionBtnTitleColorSet(actionButtonTitleColor);
DHAlertTextContentView *contentView = [DHAlertTextContentView alertTextContentViewWithText:content contentPlaceHolder:contentPlaceHolder];
maker.alertContentViewSet(contentView);
}];
controller.didOnclickActionItemCallback = callback;
[controller showInController:formController];
return controller;
}
+ (instancetype)showAlertControllerWithController:(UIViewController *)formController
title:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
actionButtonTitleColor:(UIColor * __nullable)actionButtonTitleColor
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent))callback
{
DHCustomAlertController *controller = [DHCustomAlertController controllerCustomAlert:^(DHCustomAlertControllerMaker * _Nonnull maker) {
maker.alertTitleSet(title);
maker.cancelBtnTitleSet(cancelButtonTitle);
maker.actionBtnTitleSet(actionButtonTitle).actionBtnTitleColorSet(actionButtonTitleColor);
DHAlertMessageContentView *contentView = [DHAlertMessageContentView alertMessageContentViewWithMessage:message];
maker.alertContentViewSet(contentView);
}];
controller.didOnclickActionItemCallback = callback;
[controller showInController:formController];
return controller;
}
+ (instancetype)showAlertControllerWithController:(UIViewController *)formController
title:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString * __nullable)cancelButtonTitle
actionButtonTitle:(NSString * __nullable)actionButtonTitle
onclickActionItemCallback:(void (^ __nullable)(DHCustomAlertController *customAlertController, NSDictionary *__nullable dicContent))callback
{
return [self showAlertControllerWithController:formController
title:title
message:message
cancelButtonTitle:cancelButtonTitle
actionButtonTitle:actionButtonTitle
actionButtonTitleColor:nil
onclickActionItemCallback:callback];
}
@end
分类是直接放到DHCustomAlertController中,定义好一些方法,方便使用
这里主要有两类 显示输入框 和 显示提示(其实就是没有输入框)
使用
输入框的使用
[DHCustomAlertController showAlertTextControllerWithController:formController
title:@"审核意见"
content:nil
contentPlaceHolder:@"请输入..."
cancelButtonTitle:@"取消"
actionButtonTitle:@"确认"
actionButtonTitleColor:kUIColorFromRGB(0x3478F6)
onclickActionItemCallback:^(DHCustomAlertController * _Nonnull customAlertController, NSDictionary * _Nonnull dicContent) {
NSLog(@"dicContent : %@", dicContent);
[customAlertController dismissViewController];
}];
提示的使用
[DHCustomAlertController showAlertControllerWithController:formController title:@"提示" message:@"是否删除本条备案信息?" cancelButtonTitle:@"取消" actionButtonTitle:@"删除" onclickActionItemCallback:^(DHCustomAlertController * _Nonnull customAlertController, NSDictionary * _Nullable dicContent) {
[customAlertController dismissViewController];
}];
这里注意使用输入框后弹出键盘的高度问题,我们项目中使用了IQKeyboardManager第三方,并且输入框是放到controller中的 所有不会有遮挡的问题
ps:这是我同事写的,被我盗用了....