一个简单的键盘回收和高度计算的小处理。
原理是:通过相应的子页面block代码块,获取到当前view上的textField相对于keyWindow上的frame,然后返回给当主页面判断高度来出来遮挡逻辑。
页面:
1.主页面代码
#import "MineChongZhiViewController.h"
#import "BaseNavView.h"
#import "ChongZiBtnBkg.h"
#import "ChongZiChooseJinErBkg.h"
#import "ChongZiChooseLiKaBkg.h"
@interface MineChongZhiViewController ()
{
NSInteger _isShowNum;
CGFloat _cellWidth, _cellHeight, _topHeight;
}
@property(nonatomic,strong) BaseNavView *naviView;
@property(nonatomic,strong) UIView *xiaBkg;
@property(nonatomic,strong) ChongZiBtnBkg *btnBkg;
@property(nonatomic,strong) UIView *lineBKGView, *showLineView;
@property(nonatomic,strong) UIScrollView *mScrollview;
@property(nonatomic,strong) UIView *contenBkg;
@property(nonatomic,strong) UILabel *yENumL, *yENameL;
//@property(nonatomic,strong) ChongZiChooseMoneyBkg *leftBkg;
@property(nonatomic,strong) ChongZiChooseJinErBkg *leftBkg;
@property(nonatomic,strong) ChongZiChooseLiKaBkg *rightBkg;
@property(nonatomic,assign) CGRect curRect;
@end
@implementation MineChongZhiViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"小金库";
//导航栏
[self drawNavi];
//主页面
[self drawMainView];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self addKeyboardObserver];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
}
-(void)addKeyboardObserver{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark --画导航栏--
-(void)drawNavi{
CGFloat heightNavi = [[UIApplication sharedApplication] statusBarFrame].size.height + self.navigationController.navigationBar.frame.size.height;
self.naviView = [[BaseNavView alloc]init];
[self.view addSubview:self.naviView];
self.naviView.backgroundColor = [UIColor whiteColor];
self.naviView.userInteractionEnabled = YES;
[self.naviView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view.mas_top);
make.left.mas_equalTo(self.view.mas_left);
make.right.mas_equalTo(self.view.mas_right);
make.height.mas_equalTo(heightNavi);
}];
self.naviView.titL.text = @"小金库";
self.naviView.titL.font = TEXT_BIG_S_FONT20;
self.naviView.titL.textColor = TEXT_HEX_BLOCK_66;
self.naviView.rightL.textColor = TEXT_RED_Price ;
self.naviView.rightL.text = @"充值记录";
self.naviView.lineView.backgroundColor = TEXT_HEX_GRAY_144;
self.naviView.leftImgv.image = [UIImage imageNamed:@"fanhui_icon"];
[self.naviView.leftImgv mas_updateConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(9, 17));
}];
__block typeof(self) mSelf = self;
self.naviView.BaseNavViewBlock = ^(BtnType type) {
if (type == Left_Nav_Btn) {
[mSelf.navigationController popViewControllerAnimated:YES];
}
if (type == Right_Nav_Btn) {
NSLog(@"选择了右键按钮");
}
};
}
#pragma mark -- 画图 主体框架 --
-(void)drawMainView{
self.yENumL = [[UILabel alloc]init];
[self.view addSubview:self.yENumL];
self.yENumL.font = [UIFont systemFontOfSize:40];
self.yENumL.textColor = TEXT_RED_Price;
[self.yENumL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.naviView.mas_bottom).mas_offset(50);
make.centerX.mas_equalTo(self.view.mas_centerX);
make.left.mas_greaterThanOrEqualTo(self.view.mas_left).mas_offset(15);
}];
self.yENumL.text = @"¥500";
self.yENameL = [[UILabel alloc]init];
[self.view addSubview:self.yENameL];
self.yENameL.font = TEXT_BIG_S_FONT20;
self.yENameL.textColor = TEXT_RED_Price;
[self.yENameL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.yENumL.mas_bottom).mas_offset(20);
make.centerX.mas_equalTo(self.view.mas_centerX);
make.left.mas_greaterThanOrEqualTo(self.view.mas_left).mas_offset(15);
}];
self.yENameL.text = @"当前余额";
CGFloat w_s = SCREEN_WIDTH-15*2;
CGFloat h_s = 440/375.0*w_s;
self.xiaBkg = [[UIView alloc]init];
self.xiaBkg.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.xiaBkg];
self.xiaBkg.layer.shadowColor = TEXT_HEX_GRAY_144.CGColor;//shadowColor阴影颜色
self.xiaBkg.layer.shadowOffset = CGSizeMake(1,-2);
self.xiaBkg.layer.shadowOpacity = 0.8;//阴影透明度,默认0
self.xiaBkg.layer.shadowRadius = 4;
[self.xiaBkg mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.view.mas_bottom);
make.left.mas_equalTo(self.view.mas_left).mas_offset(15);
make.right.mas_equalTo(self.view.mas_right).mas_offset(-15);
make.height.mas_equalTo(h_s);
}];
[self.xiaBkg layoutIfNeeded];
//按钮
[self drawBtnView];
//显示内容
[self drawDownView];
}
-(void)drawBtnView{
self.btnBkg = [[ChongZiBtnBkg alloc]init];
[self.xiaBkg addSubview:self.btnBkg];
self.btnBkg.backgroundColor = [UIColor whiteColor];
self.btnBkg.userInteractionEnabled = YES;
[self.btnBkg mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.xiaBkg.mas_top);
make.left.mas_equalTo(self.xiaBkg.mas_left);
make.right.mas_equalTo(self.xiaBkg.mas_right);
make.bottom.mas_equalTo(self.btnBkg.btnBkgV.mas_bottom);
}];
__block typeof(self) mSelf = self;
self.btnBkg.ClickBlock = ^(UIButton *curBtn, NSInteger type) {
if (type == 0) {
NSLog(@"在线充值");
mSelf.mScrollview.contentOffset = CGPointMake(0, 0);
}
if (type == 1) {
NSLog(@"券卡充值");
mSelf.mScrollview.contentOffset = CGPointMake(SCREEN_WIDTH-30, 0);
}
[mSelf.view endEditing:YES];
[mSelf getDataFromSeverce];
};
}
-(void)getDataFromSeverce{
}
-(void)drawDownView{
self.mScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40+40)] ;
self.mScrollview.userInteractionEnabled = YES;
self.mScrollview.bounces = YES;
self.mScrollview.backgroundColor = [UIColor clearColor];
self.mScrollview.showsHorizontalScrollIndicator = NO;
self.mScrollview.showsVerticalScrollIndicator = NO;
self.mScrollview.scrollEnabled = NO;
[self.xiaBkg addSubview:self.mScrollview];
[self.mScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.btnBkg.mas_bottom);
make.left.right.mas_equalTo(self.xiaBkg);
make.bottom.mas_equalTo(self.xiaBkg.mas_bottom);
}];
[self.mScrollview layoutIfNeeded];
self.contenBkg = [[UIView alloc]init];
[self.mScrollview addSubview:self.contenBkg];
self.contenBkg.userInteractionEnabled = YES;
self.contenBkg.backgroundColor = [UIColor clearColor];
[self.contenBkg mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.mScrollview);
make.width.mas_equalTo(self.xiaBkg.mas_width);
make.height.mas_equalTo(self.mScrollview.mas_height);
}];
[self.contenBkg layoutIfNeeded];
//画图
[self drawLeftView];
[self drawRightView];
[self.contenBkg mas_remakeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self.mScrollview);
make.right.mas_equalTo(self.rightBkg.mas_right);
make.height.mas_equalTo(self.mScrollview.mas_height);
}];
[self.contenBkg layoutIfNeeded];
}
#pragma mark -- 画左侧充值 --
-(void)drawLeftView{
self.leftBkg = [[ChongZiChooseJinErBkg alloc] init];
[self.contenBkg addSubview:self.leftBkg];
[self.leftBkg mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.left.bottom.mas_equalTo(self.contenBkg);
make.width.mas_equalTo(self.xiaBkg.mas_width);
}];
__block typeof(self)mSelf =self;
self.leftBkg.selfFrameBlock = ^(CGRect selfFrame) {
mSelf.curRect = selfFrame;
};
}
#pragma mark -- 画右侧卡券码 --
-(void)drawRightView{
self.rightBkg = [[ChongZiChooseLiKaBkg alloc] init];
[self.contenBkg addSubview:self.rightBkg];
[self.rightBkg mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.mas_equalTo(self.contenBkg);
make.left.mas_equalTo(self.leftBkg.mas_right);
make.width.mas_equalTo(self.xiaBkg.mas_width);
}];
__block typeof(self)mSelf =self;
self.rightBkg.selfFrameBlock = ^(CGRect selfFrame) {
mSelf.curRect = selfFrame;
};
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
//获取键盘起始位置、大小信息
CGRect kbRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
//40是输入框的高度
//将试图的Y坐标向上移动offset个单位,以使界面腾出开的地方用于软键盘的显示
CGFloat offSet = self.curRect.origin.y + 40 - (keyWindow.frame.size.height - kbRect.size.height);
//获取键盘弹出动画时间
NSValue *animationDurationValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//获取键盘弹出动画类型
NSValue *animationCurveValue = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve animationCurve;
[animationCurveValue getValue:&animationCurve];
[UIView setAnimationCurve:animationCurve];
if (offSet > 0.01) {
@weakify(self);
[UIView animateWithDuration:0.1 animations:^{
@strongify(self);
CGRect rect = self.view.frame;
[self.view setFrame:CGRectMake(rect.origin.x
, -offSet
, rect.size.width
, rect.size.height)];
}];
}else{
@weakify(self);
[UIView animateWithDuration:animationDuration
animations:^{
@strongify(self);
CGRect rect = self.view.frame;
[self.view setFrame:CGRectMake(rect.origin.x
,0
, rect.size.width
, rect.size.height)];
}];
}
}
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
//获取键盘起始位置、大小信息
//获取键盘弹出动画时间
NSValue *animationDurationValue = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//获取键盘弹出动画类型
NSValue *animationCurveValue = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
UIViewAnimationCurve animationCurve;
[animationCurveValue getValue:&animationCurve];
[UIView setAnimationCurve:animationCurve];
@weakify(self);
[UIView animateWithDuration:animationDuration
animations:^{
@strongify(self);
CGRect rect = self.view.frame;
[self.view setFrame:CGRectMake(rect.origin.x
,0
, rect.size.width
, rect.size.height)];
}];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@end
2.左侧视图.h
#import <UIKit/UIKit.h>
@interface ChongZiChooseJinErBkg : UIView<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,copy) void(^sureBlock)(NSInteger type);
@property(nonatomic,strong) UIView *bkgView, *xiaBkg;
@property(nonatomic,strong) UICollectionView *myColV;
@property(nonatomic,strong) UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong) NSArray *arrName;
@property(nonatomic,strong) UILabel *tsL;
@property(nonatomic,strong) UIButton *sureBtn;
@property(nonatomic,assign) CGRect selfFrame;
@property(nonatomic,copy) void(^selfFrameBlock)(CGRect selfFrame);
@end
左侧视图.m
#import "ChongZiChooseJinErBkg.h"
#import "MineChongZhiColCell.h"
#import "MineChongZhiNumColCell.h"
@interface ChongZiChooseJinErBkg ()<UITextFieldDelegate>
@property(nonatomic,strong) NSMutableArray *arr_sel;
@property(nonatomic,strong) NSString *writeStr;
@property(nonatomic,strong) UITextField *tempTF;
@end
@implementation ChongZiChooseJinErBkg
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self){
self.backgroundColor = [UIColor whiteColor];
self.arrName = @[@"500",@"1000",@"2000"];
self.arr_sel = [@[@"YES",@"NO",@"NO",@"NO"] mutableCopy];
[self creatView];
}
return self;
}
-(void)creatView{
self.bkgView = [[UIView alloc]init];
[self addSubview:self.bkgView];
self.bkgView.userInteractionEnabled = YES;
self.bkgView.backgroundColor = [UIColor whiteColor];
[self.bkgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.right.mas_equalTo(self.mas_right);
make.left.mas_equalTo(self.mas_left);
make.height.mas_equalTo(self.mas_height);
}];
[self.bkgView layoutIfNeeded];
[self drawCollectionView];
[self drawXiaView];
}
-(void)drawCollectionView{
CGFloat cell_w = (SCREEN_WIDTH-5*15)/2.0;
CGFloat cell_h = 90/150.0 *cell_w;
//画商品collectionview
self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
self.flowLayout.itemSize = CGSizeMake(cell_w, cell_h);
self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.flowLayout.minimumLineSpacing = 15;
self.flowLayout.minimumInteritemSpacing = 15;
self.flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 0, 15);
self.myColV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH) collectionViewLayout:self.flowLayout];
self.myColV.delegate = self;
self.myColV.dataSource = self;
self.myColV.bounces = YES;
self.myColV.backgroundColor = [UIColor whiteColor];
self.myColV.showsHorizontalScrollIndicator = NO;
self.myColV.showsVerticalScrollIndicator = NO;
[self.bkgView addSubview:self.myColV];
[self.myColV mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(0);
make.left.mas_equalTo(0);
make.right.mas_equalTo(0);
make.height.mas_equalTo(cell_h*2+15+15);
}];
// [self.zpCV layoutIfNeeded];
[self.myColV registerClass:[MineChongZhiColCell class] forCellWithReuseIdentifier:@"MineChongZhiColCell"];
[self.myColV registerClass:[MineChongZhiNumColCell class] forCellWithReuseIdentifier:@"MineChongZhiNumColCell"];
}
-(void)drawXiaView{
self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bkgView addSubview:self.sureBtn];
self.sureBtn.backgroundColor = TEXT_RED_Price;
self.sureBtn.layer.cornerRadius = 50/2.0;
self.sureBtn.titleLabel.font = TEXT_BIG_S_FONT20;
[self.sureBtn setTitle:@"确认充值" forState:UIControlStateNormal];
[self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.sureBtn addTarget:self action:@selector(clickedJumpPage:) forControlEvents:UIControlEventTouchUpInside];
[self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.bkgView.mas_bottom).mas_offset(-35);
make.left.mas_equalTo(self.bkgView.mas_left).mas_offset(15);
make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-15*4, 50));
}];
self.tsL = [[UILabel alloc]init];
self.tsL.font = TEXT_LIT_S_FONT12;
self.tsL.textColor = TEXT_RED_Price;
[self.bkgView addSubview:self.tsL];
self.tsL.text = @"注:赠送优惠的卡券请在规定时间内使用";
self.tsL.numberOfLines = 2;
[self.tsL mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.sureBtn.mas_top).mas_offset(-15);
make.left.mas_equalTo(self.bkgView.mas_left).mas_offset(15);
make.right.mas_lessThanOrEqualTo(self.bkgView.mas_right).mas_offset(-15);
}];
}
-(void)setCpModelArr:(NSMutableArray *)cpModelArr{
// self.arrData = cpModelArr;
// [self.zpCV reloadData];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 4;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row<=2) {
MineChongZhiColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineChongZhiColCell" forIndexPath:indexPath];
cell.numL.text = self.arrName[indexPath.row] ;
cell.index = indexPath;
cell.isChoose = [self.arr_sel[indexPath.row] boolValue];
cell.actBlock = ^(NSInteger type) {
for (int i = 0; i<self.arr_sel.count; i ++) {
// [self.tempTF resignFirstResponder];
if (i == type) {
self.arr_sel[i] = @"YES";
}else{
self.arr_sel[i] = @"NO";
}
}
MineChongZhiNumColCell *cell4 = (MineChongZhiNumColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
cell4.isChoose = NO;
[cell4.moneyTF resignFirstResponder];
[self.myColV reloadData];
};
return cell;
}else{
MineChongZhiNumColCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MineChongZhiNumColCell" forIndexPath:indexPath];
cell.index = indexPath;
cell.moneyTF.delegate = self;
cell.moneyTF.text = self.writeStr;
cell.isChoose = [self.arr_sel[indexPath.row] boolValue];
cell.actBlock = ^(NSInteger type) {
self.arr_sel = [@[@"NO",@"NO",@"NO",@"YES"] mutableCopy];
};
return cell;
}
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}
#pragma mark ---textField代理事件---
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"进入编辑状态");
self.tempTF = textField;
//进行视图切换
return YES;
}
//开始输入文字
- (void)textFieldDidBeginEditing:(UITextField *)textField{
self.tempTF = textField;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shouldChangeTextField:) name:@"UITextFieldTextDidChangeNotification" object:textField];
self.arr_sel = [@[@"NO",@"NO",@"NO",@"YES"] mutableCopy];
textField.returnKeyType = UIReturnKeyDone;
[self changeColor];
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
self.selfFrame = [textField convertRect:textField.frame toView:keyWindow];
self.selfFrameBlock(self.selfFrame);
}
//文字发生改变
- (void)shouldChangeTextField:(NSNotification *)obj{
UITextField *tf = (UITextField *)obj.object;
self.writeStr = tf.text;
[self changeColor];
}
//键盘确定搜索的点击事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(@"点击搜索的逻辑");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"完成编辑改变视图样式");
}
-(void)changeColor{
MineChongZhiColCell *cell1 = (MineChongZhiColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell1.isChoose = NO;
MineChongZhiColCell *cell2 = (MineChongZhiColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
cell2.isChoose = NO;
MineChongZhiColCell *cell3 = (MineChongZhiColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
cell3.isChoose = NO;
MineChongZhiNumColCell *cell4 = (MineChongZhiNumColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
cell4.isChoose = YES;
}
#pragma mark -- 确认付款 --
-(void)clickedJumpPage:(UIButton *)btn{
MineChongZhiNumColCell *cell4 = (MineChongZhiNumColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
[cell4.moneyTF resignFirstResponder];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
MineChongZhiNumColCell *cell4 = (MineChongZhiNumColCell *)[self.myColV cellForItemAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
[cell4.moneyTF resignFirstResponder];
}
-(NSMutableArray *)arr_sel{
if (!_arr_sel) {
_arr_sel = [NSMutableArray array];
}
return _arr_sel;
}
3.右侧视图.h
#import <UIKit/UIKit.h>
@interface ChongZiChooseLiKaBkg : UIView
@property(nonatomic,copy) void(^sureBlock)(NSInteger type);
@property(nonatomic,strong) UIView *bkgView, *line_one_view;
@property(nonatomic,strong) UIImageView *iconImgv;
@property(nonatomic,strong) UITextField *userTF;
@property(nonatomic,strong) UILabel *tsL, *descL;
@property(nonatomic,strong) UIButton *sureBtn;
@property(nonatomic,assign) CGRect selfFrame;
@property(nonatomic,copy) void(^selfFrameBlock)(CGRect selfFrame);
@end
右侧视图.m
#import "ChongZiChooseLiKaBkg.h"
@interface ChongZiChooseLiKaBkg ()<UITextFieldDelegate>
@end
@implementation ChongZiChooseLiKaBkg
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self){
self.backgroundColor = [UIColor whiteColor];
[self creatView];
}
return self;
}
-(void)creatView{
self.bkgView = [[UIView alloc]init];
[self addSubview:self.bkgView];
self.bkgView.userInteractionEnabled = YES;
self.bkgView.backgroundColor = [UIColor whiteColor];
[self.bkgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.mas_top);
make.right.mas_equalTo(self.mas_right);
make.left.mas_equalTo(self.mas_left);
make.height.mas_equalTo(self.mas_height);
}];
[self.bkgView layoutIfNeeded];
//
self.iconImgv = [[UIImageView alloc]init];
self.iconImgv.userInteractionEnabled = YES;
[self.bkgView addSubview:self.iconImgv];
[self.iconImgv mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.bkgView.mas_top).mas_offset(15);
make.left.mas_equalTo(self.bkgView.mas_left).mas_offset(15);
make.size.mas_equalTo(CGSizeMake(30, 23));
}];
self.iconImgv.image = [UIImage imageNamed:@"xiepinglun"];
self.userTF = [[UITextField alloc]init];
[self.bkgView addSubview:self.userTF];
self.userTF.backgroundColor = [UIColor clearColor];
self.userTF.placeholder = @"请输入礼卡卡密";
self.userTF.tintColor = TEXT_HEX_GRAY_144;
self.userTF.textColor = TEXT_HEX_BLOCK_2;
self.userTF.font = TEXT_MIDD_S_FONT15;
self.userTF.delegate = self;
self.userTF.clearButtonMode = UITextFieldViewModeWhileEditing;
[self.userTF mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.mas_equalTo(self.iconImgv.mas_centerY);
make.left.mas_equalTo(self.iconImgv.mas_right).mas_offset(10);
make.right.mas_equalTo(self.bkgView.mas_right).mas_offset(-15);
make.height.mas_equalTo(40);
}];
self.line_one_view = [[UIView alloc]init];
self.line_one_view.userInteractionEnabled = YES;
self.line_one_view.backgroundColor = TEXT_HEX_GRAY_229;
[self.bkgView addSubview:self.line_one_view];
[self.line_one_view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.iconImgv.mas_bottom).mas_offset(12);
make.left.mas_equalTo(self.iconImgv.mas_left);
make.right.mas_equalTo(self.bkgView.mas_right).mas_offset(-15);
make.height.mas_equalTo(1);
}];
self.tsL = [[UILabel alloc]init];
self.tsL.font = TEXT_LIT_S_FONT12;
self.tsL.textColor = TEXT_RED_Price;
[self.bkgView addSubview:self.tsL];
self.tsL.text = @"请正确输入10位礼卡卡密";
self.tsL.numberOfLines = 2;
[self.tsL mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.line_one_view.mas_top).mas_offset(15);
make.left.mas_equalTo(self.bkgView.mas_left).mas_offset(15);
make.right.mas_lessThanOrEqualTo(self.bkgView.mas_right).mas_offset(-15);
}];
self.sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.bkgView addSubview:self.sureBtn];
self.sureBtn.backgroundColor = TEXT_RED_Price;
self.sureBtn.layer.cornerRadius = 50/2.0;
self.sureBtn.titleLabel.font = TEXT_BIG_S_FONT20;
[self.sureBtn setTitle:@"确认充值" forState:UIControlStateNormal];
[self.sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.sureBtn addTarget:self action:@selector(clickedJumpPage:) forControlEvents:UIControlEventTouchUpInside];
[self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.bkgView.mas_bottom).mas_offset(-35);
make.left.mas_equalTo(self.bkgView.mas_left).mas_offset(15);
make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-15*4, 50));
}];
self.descL = [[UILabel alloc]init];
self.descL.font = TEXT_LIT_S_FONT12;
self.descL.textColor = TEXT_RED_Price;
[self.bkgView addSubview:self.descL];
self.descL.text = @"充卡成功后,可畅享珍绿尚品在线商城及线下门店一卡便捷消费";
self.descL.numberOfLines = 2;
[self.descL mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.mas_equalTo(self.sureBtn.mas_top).mas_offset(-15);
make.left.mas_equalTo(self.bkgView.mas_left).mas_offset(15);
make.right.mas_lessThanOrEqualTo(self.bkgView.mas_right).mas_offset(-15);
}];
}
#pragma mark -- 确认充值 --
-(void)clickedJumpPage:(UITapGestureRecognizer *)tap{
[self.userTF resignFirstResponder];
}
#pragma mark ---textField代理事件---
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"进入编辑状态");
//进行视图切换
return YES;
}
//开始输入文字
- (void)textFieldDidBeginEditing:(UITextField *)textField{
// [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(shouldChangeTextField:) name:@"UITextFieldTextDidChangeNotification" object:textField];
textField.returnKeyType = UIReturnKeyDone;
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
self.selfFrame = [textField convertRect:textField.frame toView:keyWindow];
self.selfFrameBlock(self.selfFrame);
}
//文字发生改变
//- (void)shouldChangeTextField:(NSNotification *)obj{
// UITextField *tf = (UITextField *)obj.object;
//
//}
//键盘确定搜索的点击事件
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSLog(@"点击搜索的逻辑");
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"完成编辑改变视图样式");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.userTF resignFirstResponder];
}
主要是通过监听键盘出现和消失来实现的