UIView * _toolView; //工具栏 UITextField *textField;// 输入框 呼出键盘用
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 64, 120, 60)];
textField.placeholder = @"测试";
[self.view addSubview:textField];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil];
//初始化工具栏
_toolView = [[UIView alloc]init];
_toolView.frame = CGRectMake(0, screen_Height, screen_Width, 50);
[self.view addSubview:_toolView];
UIButton *losebtn = [UIButton buttonWithType:UIButtonTypeCustom];
losebtn.frame = CGRectMake(20, 0, 50, 50);
[losebtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[losebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[losebtn setTitle:@"收起" forState:UIControlStateNormal];
[_toolView addSubview:losebtn];
UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[imageBtn setTitle:@"图片" forState:UIControlStateNormal];
imageBtn.frame = CGRectMake(screen_Width-100, 0, 50, 50);
[imageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[imageBtn addTarget:self action:@selector(imageBtnClick) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:imageBtn];
UIButton *cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[cameraBtn setTitle:@"相机" forState:UIControlStateNormal];
cameraBtn.frame = CGRectMake(screen_Width-50, 0, 50, 50);
[cameraBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cameraBtn addTarget:self action:@selector(cameraBtnClick) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:cameraBtn];
UIButton *canclebtn = [UIButton buttonWithType:UIButtonTypeCustom];
[canclebtn setTitle:@"取消" forState:UIControlStateNormal];
canclebtn.frame = CGRectMake(screen_Width-150, 0, 50, 50);
[canclebtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[canclebtn addTarget:self action:@selector(canclebtnBtnClick) forControlEvents:UIControlEventTouchUpInside];
[_toolView addSubview:canclebtn];
}
#pragma mark 当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//键盘弹出时显示工具栏
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
float keyBoardHeight = keyboardRect.size.height;
// NSLog(@"%ld",(long)keyBoardHeight);
[UIView animateWithDuration:0.1 animations:^{
_toolView.frame = CGRectMake(0, screen_Height-keyBoardHeight-50, screen_Width, 50);
}];
}
#pragma mark 当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
//键盘消失时 隐藏工具栏
[UIView animateWithDuration:0.1 animations:^{
_toolView.frame = CGRectMake(0, screen_Height+50, screen_Width, 50);
}];
}
- (void)btnClick{
[textField resignFirstResponder];
}
- (void)imageBtnClick{
}
- (void)cameraBtnClick{
}
- (void)canclebtnBtnClick{
}
UIToolbar *keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(doneClicked:)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
txtField.inputAccessoryView = keyboardDoneButtonView;
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有