ToolBar跟随键盘弹出和隐藏
实现效果如下:

代码实现如下:
//
// ViewController.m
// PopToolBarDemo
//
// Created by 思 彭 on 2017/7/20.
// Copyright © 2017年 思 彭. All rights reserved.
// #import "ViewController.h" @interface ViewController ()<UITextFieldDelegate> @property (weak, nonatomic) IBOutlet UITextField *myTextField;
@property (nonatomic, strong) UIToolbar *toolBarView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.myTextField.delegate = self; // 监听键盘的弹出
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; CGSize size = self.view.frame.size;
// 创建一个工具条,并设置它的大小和位置
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(, [UIScreen mainScreen].bounds.size.height, size.width, )];
self.toolBarView = toolbar;
// 设置工具条的style
[toolbar setBarStyle:UIBarStyleDefault]; // 创建使用文本标题的UIBarButtonItem
// UIBarButtonItem* leftItem = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(onClick:)]; UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(onClick:)];
[spaceItem setTag:]; // 创建使用自定义图片的UIBarButtonItem
UIBarButtonItem* centerItem = [[UIBarButtonItem alloc]
initWithImage:[[UIImage imageNamed:@"exam_stopwatch_icon"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]
style:UIBarButtonItemStylePlain
target:self
action:@selector(onClick:)];
[centerItem setTag:];
// 创建使用系统图标的UIBarButtonItem
UIBarButtonItem* rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(onClick:)];
[rightItem setTag:];
// 为工具条设置工具按钮 注意设置间距
NSArray* barButtonItems = [NSArray arrayWithObjects:spaceItem, centerItem, spaceItem, rightItem,spaceItem, nil]; [toolbar setItems:barButtonItems animated:YES];
// 将工具条添加到当前应用的界面中
[self.view addSubview:toolbar];
} - (void)onClick: (UIBarButtonItem *)item {
NSInteger tag = [item tag];
switch (tag) {
case :
NSLog(@"第一个");
break;
case :
NSLog(@"第二个");
break;
default:
break;
}
} - (void)textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"textField.text = %@",textField.text);
} - (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self.myTextField resignFirstResponder];
return YES;
} - (void)keyBoardWillChangeFrame:(NSNotification*)notification{ // 键盘显示\隐藏完毕的frame
CGRect frame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 动画时间
CGFloat duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; if (frame.origin.y == [UIScreen mainScreen].bounds.size.height) {
// 没有弹出键盘
[UIView animateWithDuration:duration animations:^{ self.toolBarView.transform = CGAffineTransformIdentity; }];
} else{ // 弹出键盘 // 工具条往上移动258
[UIView animateWithDuration:duration animations:^{ self.toolBarView.transform = CGAffineTransformMakeTranslation(, -frame.size.height-);
}];
}
} @end
ToolBar跟随键盘弹出和隐藏的更多相关文章
- 【转】swift实现ios类似微信输入框跟随键盘弹出的效果
swift实现ios类似微信输入框跟随键盘弹出的效果 为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出,当你点击其他地方时,输入框又会 ...
- swift实现ios类似微信输入框跟随键盘弹出的效果
封面(图文无关) 为什么要做这个效果 在聊天app,例如微信中,你会注意到一个效果,就是在你点击输入框时输入框会跟随键盘一起向上弹出,当你点击其他地方时,输入框又会跟随键盘一起向下收回,二者完全无缝连 ...
- Android 判断软键盘弹出并隐藏的简单完美解决方案
最近项目中有一个编辑框,下面是个ListView.在触发编辑框弹出软键盘后,ListView还能滑动,并且ListView的item还能响应单击.这样的体验效果很不好.于是便想在滑动或单击item时判 ...
- IOS键盘弹出、隐藏
IOS键盘 UIKeyboardFrameBeginUserInfoKey:动画开始前键盘的size UIKeyboardFrameEndUserInfoKey:动画结束后键盘的size - (voi ...
- (二十四)监听键盘的通知和键盘弹出隐藏的View移动
让控制器监听键盘的通知,注意谁监听,谁的dealloc方法中就要remove,如果非ARC还要调用父类的dealloc方法. //监听键盘的操作: [[NSNotificationCenter def ...
- Android 软键盘弹出时把原来布局顶上去的解决方法
键盘弹出时,会将布局底部的导航条顶上去. 解决办法: 在mainfest.xml中,在和导航栏相关的activity中加: <activity android:name=& ...
- Android软键盘弹出时布局问题
最近项目需要做一个类似聊天室的模块,基于Socket实现的,这部分稍后一段时间再做总结,功能上的相关点都实现了小例子也做出来了,最后发现一个比较腻歪的问题就是软键盘弹出时总是会把标题“挤出”屏幕,(无 ...
- iOS学习——键盘弹出遮挡输入框问题解决方案
在iOS或Android等移动端开发过程中,经常遇到很多需要我们输入信息的情况,例如登录时要输入账号密码.查询时要输入查询信息.注册或申请时需要填写一些信息等都是通过我们键盘来进行输入的,在iOS开发 ...
- Android软键盘弹出,覆盖h5页面输入框问题
之前我们在使用vue进行 h5 表单录入的过程中,遇到了Android软键盘弹出,覆盖 h5页面 输入框 问题,在此进行回顾并分享给大家: 系统:Android 条件:当输入框在可视区底部或者偏下的位 ...
随机推荐
- selenium八种定位元素方法
1.driver.find_element_by_id('su') 定位到元素的id一般id是唯一的,可以精确定位到元素 2.driver.find_element_by_name() 通过元素的na ...
- 解决Python中出现的问题: “You are using pip version 9.0.1, however version 19.2.3 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.”
1. 一开始我在使用Pycharm时,导入numpy库,发现导入错误: Non-zero exit code (1) 2. 于是我通过更新的方法来解决,哪知道在更新的时候也出现了错误,错误如下图: 这 ...
- C. Vasily the Bear and Sequence Codeforces 336C(枚举,思维)
C. Vasily the Bear and Sequence time limit per test 1 second memory limit per test 256 megabytes inp ...
- Python&Selenium&Unittest&BeautifuReport 自动化测试并生成HTML自动化测试报告
一.摘要 本篇博文将介绍如何借助BeautifulReport和HTML模版,生成HTML测试报告的BeautifulReport 源码Clone地址为 https://github.com/Test ...
- java动态代理框架
java动态代理是一个挺有意思的东西,他有时候可以被使用的很灵活.像rpc的调用,调用方只是定义的一个接口,动态代理让他匹配上对应的不同接口:mybatis内部的实现,编码时,只是实 ...
- SpringMVC防止重复提交
作者:mylovepan 推荐:GOODDEEP 问题描述: 现在的网站在注册步骤中,由于后台要处理大量信息,造成响应变慢(测试机器性能差也是造成变慢的一个因素),在前端页面提交信息之前,等待后端响应 ...
- django设置debug设为False,前端样式出现错乱或静态文件404问题。
部署到生产环境中遇到的坑大部分都是引用静态文件带来的, setting设置里面debug设为False, 需要设置ALLOWED_HOSTS=["服务器ip"],不然运行不了的. ...
- C# 操作服务命令
安装服务 @echo.服务启动...... @echo off @sc create 服务名称 binPath= " exe地址" @net start 服务名称 @sc conf ...
- 解决oracle 32位64位的问题
工具-选项-项目和解决方案-勾选第一个
- Python基础--01小项目体现的基础知识
part1:猜拳游戏 #coding=utf-8 #当有汉语时可能编译器不认识,需要定义代码 ''' 多行注释 写这个程序是为了熟悉python的基本语法 这是第一个小例子包含简单的if判断,循环和输 ...