刚开始学习ios开发的童鞋们会有这样的疑问,点了textfield会弹出键盘。但是键盘收不回去我的button怎么办,挡住了啊!?有木有今天就来教3种无脑方法
1.系统代理
记得在.h文件的协议里加上
<UITextFieldDelegate>
写方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
returnYES;
}
然后创建textfield,并在下面添加
UITextField * f1 = [[UITextFieldalloc]initWithFrame:CGRectMake(150,100,160,30)];
f1.delegate =self;
之后键盘就可以回收了
2.关联button
写方法
-(void)buttonAction:(id)sender//button触发事件的方法
{
UITextField * field1 = (UITextField *)[self.containerViewviewWithTag:1];
[field1 resignFirstResponder];
}
创建一个button
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50,50,50,50)];
关联 [buttonaddTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
这样做的话必须把按钮放在弹出键盘挡不到的地方。
3.一个大小为全屏的button
创建一个大小和mainscreen一样的button
同2方法关联然后就ok了~