RejectView.h
UIPlaceHolderTV的创建连接
typedef void(^SureBlocks) (NSString *rejectReasonText);
/**
驳回view
*/
@interface RejectView : UIView
/**
显示
*/
- (void)showRejectView;
/**
回调block
*/
@property SureBlocks sureBlock;
/**
文本框
*/
@property UIPlaceHolderTV *reasonTextView;
/**
标题
*/
@property UILabel *titleLabel;
RejectView.m
#define WIDTH [UIApplication sharedApplication].keyWindow.frame.size.width
#define HEIGHT [UIApplication sharedApplication].keyWindow.frame.size.height
@interface RejectView()
@property UIView *rejectBGView;
@end
@implementation RejectView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
if (self) {
self.alpha = 0;
self.backgroundColor = [UIColor blackColor];
self.rejectBGView = [[UIView alloc] initWithFrame:frame];
self.rejectBGView.center = self.center;
self.rejectBGView.backgroundColor = [UIColor whiteColor];
self.rejectBGView.layer.cornerRadius = 15;
self.rejectBGView.alpha = 0;
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 16, frame.size.width-60, 30)];
self.titleLabel.text = @"拒绝原因";
self.titleLabel.font = [UIFont systemFontOfSize:19];
[self.rejectBGView addSubview:self.titleLabel];
self.reasonTextView = [[UIPlaceHolderTV alloc] initWithFrame:CGRectMake(30, 55, frame.size.width-60, frame.size.height-55-45-5) adaption:NO];
self.reasonTextView.placeholder = @"请输入拒绝原因";
self.reasonTextView .font = [UIFont systemFontOfSize:14];
self.reasonTextView.textContainerInset = UIEdgeInsetsMake(10,0, 0, 0);
[self.rejectBGView addSubview: self.reasonTextView];
UIButton *cancalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[cancalBtn setTitle:@"取消" forState:UIControlStateNormal];
cancalBtn.layer.cornerRadius = 15;
cancalBtn.titleLabel.font = [UIFont systemFontOfSize:15];
cancalBtn.frame = CGRectMake(frame.size.width/2-105, frame.size.height-45, 80, 30);
[cancalBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancalBtn setBackgroundColor:[UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1]];
[self.rejectBGView addSubview:cancalBtn];
[cancalBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sureBtn.frame = CGRectMake(frame.size.width/2+25, frame.size.height-45, 80, 30);
[sureBtn setBackgroundColor:[UIColor redColor]];
sureBtn.layer.cornerRadius = 15;
sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
[sureBtn setTitle:@"确定" forState:UIControlStateNormal];
[sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
[self.rejectBGView addSubview:sureBtn];
}
return self;
}
- (void)hideRejectView{
[self getCurrentVC].navigationController.interactivePopGestureRecognizer.enabled = YES;
[self.rejectBGView endEditing:YES];
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0;
self.rejectBGView.alpha = 0;
}];
}
- (void)showRejectView{
[self getCurrentVC].navigationController.interactivePopGestureRecognizer.enabled = NO;
self.reasonTextView.text = @"";
[self.superview.superview addSubview:self.rejectBGView];
[UIView animateWithDuration:0.3 animations:^{
self.alpha = 0.5;
self.rejectBGView.alpha = 1;
}];
}
- (void)cancelAction:(UIButton *)btn{
[self hideRejectView];
}
- (void)sureAction:(UIButton *)btn{
if (self.delegate) {
[self.delegate rejectReason:self.reasonTextView.text index:self.index];
}
if (self.sureBlock) {
self.sureBlock(self.reasonTextView.text);
}
if (self.reasonTextView.text.length > 0) {
[self hideRejectView];
}
}
- (UIViewController *)getCurrentVC {
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
return currentVC;
}
- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC{
UIViewController *currentVC;
if ([rootVC presentedViewController]) {
// 视图是被presented出来的
rootVC = [rootVC presentedViewController];
}
if ([rootVC isKindOfClass:[UITabBarController class]]) {
// 根视图为UITabBarController
currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
} else if ([rootVC isKindOfClass:[UINavigationController class]]){
// 根视图为UINavigationController
currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
} else {
// 根视图为非导航类
currentVC = rootVC;
}
return currentVC;
}