在我的应用程序中,我试图在 accessoryView 上添加一个 UITextField。它应该按如下方式工作:
- 我在应用程序的用户界面中按下 UITextField
- 键盘弹出并覆盖用户界面上的 UITextField
- 为了编辑文本,我想在键盘上方的 accessoryView 中显示一个 UITextField
我在网上看了看,但我只找到了在 accessoryView 上添加按钮的东西,我希望你能帮助我找到在 accessoryView 上添加 UITextField 的解决方案。
谢谢
更新
这就是我的意思:
我希望您能更好地理解我的问题,我不是在寻找应用户 ScrollView 或其他东西的解决方案,我不想更改我的应用程序的界面...
最佳答案
在 .h 中
UIView *customtoolbar;
在 .m 的 viewdidload 中添加代码
customtoolbar=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height+50, 320, 50)];`
在此之后添加此方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[self pressdone];
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//216 is keyboard default height in portrait(162 in landscape)
[UIView animateWithDuration:0.7 animations:^{
[self addtoolbar:CGRectMake(0, self.view.frame.size.height-216-50, 320, 50)];
}];
return YES;
}
-(UIView *)addtoolbar:(CGRect )frame{
customtoolbar.frame=frame;
customtoolbar.backgroundColor=[UIColor darkGrayColor];
//give new frame to your textfield
txtfld.frame=CGRectMake(5,10, 220, 30);
[customtoolbar addSubview:txtfld];
UIButton *done=[UIButton buttonWithType:UIButtonTypeCustom];
done.frame=CGRectMake(235,10, 60, 30);
[done setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[done setTitle:@"Done" forState:UIControlStateNormal];
[done addTarget:self action:@selector(pressdone) forControlEvents:UIControlEventTouchUpInside];
[customtoolbar addSubview:done];
[self.view addSubview:customtoolbar];
return customtoolbar;
}
-(void)pressdone{
[self addtoolbar:CGRectMake(0, self.view.frame.size.height+50, 320, 50)];
//set there orignal frame of your textfield
txtfld.frame=CGRectMake(95, 170, 123, 37);
[self.view addSubview:txtfld];
[txtfld resignFirstResponder];
}
关于ios - 在 iOS 键盘上方添加 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28022425/