#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
self.window.rootViewController = navi; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h>
#import "TPKeyboardAvoidingScrollView.h" @interface RootViewController : UIViewController @property(nonatomic, assign) NSInteger id;
//@property(nonatomic, copy) NSString *name;//姓名
//@property(nonatomic, copy) NSString *bankAccount;//银行账号
//@property(nonatomic, copy) NSString *bankName;// 开户的银行
@property(nonatomic, strong) TPKeyboardAvoidingScrollView *backgroundView;//背景图 @end
#import "RootViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define TAG 10000
#define topHeight 20 @interface RootViewController ()
{
UIImageView *lineView;
}
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
//适配ios7
if([UIDevice currentDevice].systemVersion.floatValue >= 7.0) {
self.edgesForExtendedLayout = UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars = NO;
self.modalPresentationCapturesStatusBarAppearance = NO;
}
self.title = @"绑定银行卡"; _backgroundView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(, , WIDTH, [UIScreen mainScreen].bounds.size.height - )];
// _backgroundView.backgroundColor = [UIColor orangeColor];
[self.view addSubview:_backgroundView]; //初始化label
[self setupLable:CGRectMake(, topHeight, , ) title:@"开户银行:"];
[self setupLable:CGRectMake(, topHeight+, , ) title:@"开户归属地:"];
[self setupLable:CGRectMake(, topHeight+, , ) title:@"姓 名:"];
[self setupLable:CGRectMake(, topHeight+, , ) title:@"账 号:"];
[self setupLable:CGRectMake(, topHeight+, , ) title:@"验证码:"];
//初始化银行类型textField
[self setupTextField:CGRectMake(, topHeight, (WIDTH - )-, ) placeholder:@"请输入银行类型" index:];
//初始化省份textField
UITextField *provinceTF = [self setupTextField:CGRectMake(, topHeight+,(WIDTH - - (+) - )/2.0, ) placeholder:@"请输入省份" index:];
//小细线
lineView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(provinceTF.frame)+, CGRectGetMidY(provinceTF.frame), , )];
lineView.backgroundColor = [UIColor lightGrayColor];
[self.backgroundView addSubview:lineView];
//初始化地级市textField
[self setupTextField:CGRectMake(CGRectGetMaxX(lineView.frame)+, topHeight+, (WIDTH - - (+) - )/2.0, ) placeholder:@"请输入地级市" index:];
//初始化姓名textField
[self setupTextField:CGRectMake(, topHeight+, (WIDTH - )-, ) placeholder:@"请输入姓名" index:];
//初始化银行账号textField
[self setupTextField:CGRectMake(, topHeight+, (WIDTH - )-, ) placeholder:@"请输入银行账号" index:];
//初始化验证码textField(100是获取验证码按钮的宽度)
UITextField *securityCodeTF = [self setupTextField:CGRectMake(, topHeight+, (WIDTH - )- - - , ) placeholder:@"验证码" index:];
//初始化确定按钮
[self setupButton:CGRectMake((WIDTH - )/2.0,CGRectGetMaxY(securityCodeTF.frame)+ /*_backgroundView.frame.size.height - 30*/, , ) title:@"确定" image:@"确定按钮.png" selector:@selector(makeSureAction:)]; //初始化获取验证码按钮
[self setupButton:CGRectMake(CGRectGetMaxX(securityCodeTF.frame)+ , topHeight+, , ) title:@"获取验证码" image:@"矩形-2.png" selector:@selector(getSecurityCode:)]; }
/**
* 获取验证码
*/
- (void)getSecurityCode:(UIButton *)sender{
NSLog(@"获取验证码操作");
} // 点击确定按钮,提交数据
- (void)makeSureAction:(UIButton *)sender{
NSLog(@"提交前需要判断");
}
/**
* 初始化button
*/
- (void)setupButton:(CGRect)frame title:(NSString *)title image:(NSString *)imageName selector:(SEL)selectorName{
UIButton *securityCode = [UIButton buttonWithType:UIButtonTypeCustom];
securityCode.frame = frame;
securityCode.layer.cornerRadius = 5.2;
[securityCode setTitle:title forState:];
securityCode.clipsToBounds = YES;
securityCode.titleLabel.font = [UIFont systemFontOfSize:];
[securityCode setBackgroundImage:[UIImage imageNamed:imageName] forState:];
[securityCode addTarget:self action:selectorName forControlEvents:UIControlEventTouchUpInside];
[self.backgroundView addSubview:securityCode];
}
/**
* 初始化UITextField
*/
- (UITextField *)setupTextField:(CGRect)frame placeholder:(NSString *)context index:(NSInteger)tag{
UITextField *tf = [[UITextField alloc] initWithFrame:frame];
tf.font = [UIFont systemFontOfSize:];
tf.placeholder = context;
tf.tag = TAG + tag;
tf.borderStyle = UITextBorderStyleRoundedRect;
[self.backgroundView addSubview:tf];
return tf;
}
/**
* 初始化UILabel
*/
- (void)setupLable:(CGRect)frame title:(NSString*)content{
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = content;
label.font = [UIFont systemFontOfSize:];
label.textAlignment = NSTextAlignmentRight;
[self.backgroundView addSubview:label];
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)even{
[self.view endEditing:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; } @end
/**
* 第三方框架(TPKeyboardAvoiding)的代码
*/
#import <UIKit/UIKit.h> @interface TPKeyboardAvoidingScrollView : UIScrollView{ UIEdgeInsets _priorInset;
BOOL _priorInsetSaved;
BOOL _keyboardVisible;
CGRect _keyboardRect;
CGSize _originalContentSize;
} - (void)adjustOffsetToIdealIfNeeded; @end
/**
* 第三方框架(TPKeyboardAvoiding)的代码
*/
#import "TPKeyboardAvoidingScrollView.h" #define _UIKeyboardFrameEndUserInfoKey (&UIKeyboardFrameEndUserInfoKey != NULL ? UIKeyboardFrameEndUserInfoKey : @"UIKeyboardBoundsUserInfoKey") #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height @interface TPKeyboardAvoidingScrollView ()
- (UIView*)findFirstResponderBeneathView:(UIView*)view;
- (UIEdgeInsets)contentInsetForKeyboard;
- (CGFloat)idealOffsetForView:(UIView *)view withSpace:(CGFloat)space;
- (CGRect)keyboardRect;
@end @implementation TPKeyboardAvoidingScrollView - (void)setup {
_priorInsetSaved = NO;
if ( CGSizeEqualToSize(self.contentSize, CGSizeZero) ) {
self.contentSize = self.bounds.size;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
} -(id)initWithFrame:(CGRect)frame {
if ( !(self = [super initWithFrame:frame]) ) return nil;
[self setup];
return self; } -(void)awakeFromNib {
[self setup];
} -(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
} -(void)setFrame:(CGRect)frame {
[super setFrame:frame]; CGSize contentSize = _originalContentSize;
contentSize.width = MAX(contentSize.width, SCREEN_WIDTH);
contentSize.height = MAX(contentSize.height, SCREEN_HEIGHT-);
[super setContentSize:contentSize]; if ( _keyboardVisible ) {
self.contentInset = [self contentInsetForKeyboard];
}
} -(void)setContentSize:(CGSize)contentSize {
_originalContentSize = contentSize; contentSize.width = MAX(contentSize.width, contentSize.width);
contentSize.height = MAX(contentSize.height, contentSize.height);
[super setContentSize:contentSize]; if ( _keyboardVisible ) {
self.contentInset = [self contentInsetForKeyboard];
}
} - (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self findFirstResponderBeneathView:self] resignFirstResponder];
[super touchesEnded:touches withEvent:event];
} - (void)keyboardWillShow:(NSNotification*)notification {
_keyboardRect = [[[notification userInfo] objectForKey:_UIKeyboardFrameEndUserInfoKey] CGRectValue];
_keyboardVisible = YES; UIView *firstResponder = [self findFirstResponderBeneathView:self];
if ( !firstResponder ) {
// No child view is the first responder - nothing to do here
return;
} if (!_priorInsetSaved) {
_priorInset = self.contentInset;
_priorInsetSaved = YES;
} // Shrink view's inset by the keyboard's height, and scroll to show the text field/view being edited
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]]; self.contentInset = [self contentInsetForKeyboard];
[self setContentOffset:CGPointMake(self.contentOffset.x,
[self idealOffsetForView:firstResponder withSpace:[self keyboardRect].origin.y - self.bounds.origin.y])
animated:YES]; [UIView commitAnimations];
} - (void)keyboardWillHide:(NSNotification*)notification {
_keyboardRect = CGRectZero;
_keyboardVisible = NO; // Restore dimensions to prior size
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.contentInset = _priorInset;
_priorInsetSaved = NO;
[UIView commitAnimations];
} - (UIView*)findFirstResponderBeneathView:(UIView*)view {
// Search recursively for first responder
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] ) return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result ) return result;
}
return nil;
} - (UIEdgeInsets)contentInsetForKeyboard {
UIEdgeInsets newInset = self.contentInset;
CGRect keyboardRect = [self keyboardRect];
newInset.bottom = keyboardRect.size.height - ((keyboardRect.origin.y+keyboardRect.size.height) - (self.bounds.origin.y+self.bounds.size.height));
return newInset;
} -(CGFloat)idealOffsetForView:(UIView *)view withSpace:(CGFloat)space { // Convert the rect to get the view's distance from the top of the scrollView.
CGRect rect = [view convertRect:view.bounds toView:self]; // Set starting offset to that point
CGFloat offset = rect.origin.y; if ( self.contentSize.height - offset < space ) {
// Scroll to the bottom
offset = self.contentSize.height - space;
} else {
if ( view.bounds.size.height < space ) {
// Center vertically if there's room
offset -= floor((space-view.bounds.size.height)/2.0);
}
if ( offset + space > self.contentSize.height ) {
// Clamp to content size
offset = self.contentSize.height - space;
}
} if (offset < ) offset = ; return offset;
} -(void)adjustOffsetToIdealIfNeeded { // Only do this if the keyboard is already visible
if ( !_keyboardVisible ) return; CGFloat visibleSpace = self.bounds.size.height - self.contentInset.top - self.contentInset.bottom; CGPoint idealOffset = CGPointMake(, [self idealOffsetForView:[self findFirstResponderBeneathView:self] withSpace:visibleSpace]); [self setContentOffset:idealOffset animated:YES];
} - (CGRect)keyboardRect {
CGRect keyboardRect = [self convertRect:_keyboardRect fromView:nil];
if ( keyboardRect.origin.y == ) {
CGRect screenBounds = [self convertRect:[UIScreen mainScreen].bounds fromView:nil];
keyboardRect.origin = CGPointMake(, screenBounds.size.height - keyboardRect.size.height);
}
return keyboardRect;
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/ @end

iOS TPKeyboardAvoiding自动识别键盘的高度的更多相关文章

  1. 转 iOS获取软键盘的高度

    - (void)viewDidLoad { [super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter ...

  2. iOS之 利用通知(NSNotificationCenter)获取键盘的高度,以及显示和隐藏键盘时修改界面的注意事项

    我们在开发中会遇到这样的情况:调用键盘时需要界面有一个调整,避免键盘遮掩输入框. 但实现时你会发现,在不同的手机上键盘的高度是不同的.这里列举一下: //获取键盘的高度 /* iphone 6: 中文 ...

  3. 79.iOS 设备的UI规范和iOS各控件默认高度

    iOS设备的UI 规范 iPhone界面尺寸 iPhone图标尺寸 iPad的设计尺寸 iPad图标尺寸 iPhone设备尺寸分辨率比例 iPhone各设备 launch image iOS 各种控件 ...

  4. Xamarin的不归路-ios模拟器没有键盘

    ios模拟器没有键盘解决方案: 勾选上就有了. 2016年9月1日

  5. IOS TextField伴随键盘移动

    这篇文章介绍的是一个简单而又实用的小方法. 我想对于登陆时的一些效果大家应该都不会陌生. 今天就介绍一下,当开始输入TextField文本时键盘弹出TextField伴随键盘移动的实现. 先看一下演示 ...

  6. ios 弹出键盘 视图向上平移

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardwillAppear:) name:U ...

  7. ios如何判断键盘是否已经显示

    ios如何判断键盘是否已经显示   在群里看到有人问:ios如何判断键盘已经显示在界面上. 其实这个解决很简单: 写一个单例来管理键盘的状态. 这个单例在初始化方法init种监听2个事件,分别是 UI ...

  8. ios自定义数字键盘

    因为项目又一个提现的功能,textfiled文本框输入需要弹出数字键盘,首先想到的就是设置textfiled的keyboardType为numberPad,此时你会看到如下的效果:   但是很遗憾这样 ...

  9. IOS中input键盘事件支持的解决方法

    欢迎大家去我的网站详细查看http://genghongshuo.com.cn/ IOS中input键盘事件keyup.keydown.等支持不是很好, 用input监听键盘keyup事件,在安卓手机 ...

随机推荐

  1. Go项目的目录结构说明

    一.项目目录结构 GoPath    /bin    /pkg    /src project_1      project_2 ...... project_n GoPath : 相当于donet下 ...

  2. Nginx 笔记与总结(6)Location:精准匹配

    在 /usr/local/nginx/conf/nginx.conf 的 server 段中,location 表示根据 URI 来进行不同的定位:把网站的不同部分定位到不同的处理方式上,例如遇到 . ...

  3. django通过middleware计算每个页面的详细执行时间

    你可以自定义一个MiddleWare类,然后在settings.py引用这个中间件,添加到MIDDLEWARE_CLASSES里,然后在公共模板里添显示代码即可. 添加到公共模板里的代码: <d ...

  4. Rails--default_scope

    Example: default_scope where("agents.deleted = ?", false)

  5. laravel 视图组件

    假设有一个文件被多个视图需要,比如导航条: 1.在路由文件添加 View::composer('stats', function($view){ $view->with('stats', app ...

  6. 六 mybatis高级映射(一对一,一对多,多对多)

    1  订单商品数据模型 以订单商品数据为模型,来对mybaits高级关系映射进行学习.

  7. (转)Linux下MatlabCompilerRuntime的安装和使用

    1MCR简介 MCR之前是 Matlab Component Runtime的缩写,后更名为Matlab Compiler Runtime.MCR实际上是一组独立的共享库,也即是常说的动态连接库,所起 ...

  8. zepto源码--extend--学习笔记

    对象继承函数: $.extend(){},默认传递一个参数,需要继承的对象目标.函数展示: 最终实现的过程,需要调用工具函数extend,首先分析extend函数. 默认传递三个参数,继承的目标对象- ...

  9. Nodejs电影建站开发实例(下)

    作为一个真正的网站,不能没有数据的支持,下面使用的数据库为mongodb,电影可能有的数据:电影名称.导演.国家.语言.上映时间.图片.简介.视频 4.使用路由 app.js var express ...

  10. 让Eclipse不格式化数组或某段代码

    用过eclipse ctrl+shit+f的人肯定都感觉eclipse这个功能很爽. 但对于数组,有时候就不是这样了. 比如在opengl中定义一些顶点信息: int one = 0x010000; ...