ios - 在 iOS 键盘上方添加 UITextField

标签 ios objective-c keyboard

在我的应用程序中,我试图在 accessoryView 上添加一个 UITextField。它应该按如下方式工作:

  1. 我在应用程序的用户界面中按下 UITextField
  2. 键盘弹出并覆盖用户界面上的 UITextField
  3. 为了编辑文本,我想在键盘上方的 accessoryView 中显示一个 UITextField

我在网上看了看,但我只找到了在 accessoryView 上添加按钮的东西,我希望你能帮助我找到在 accessoryView 上添加 UITextField 的解决方案。

谢谢

更新

这就是我的意思:

enter image description here

我希望您能更好地理解我的问题,我不是在寻找应用户 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/

相关文章:

jquery - 使用Jquery模拟键盘按键

python - 两次单独的按键来保存和退出 while 循环

ios - Swift Struct 与 Class : what is the allowed stack size? 并将类重构为结构

objective-c - 关于 NSUserDefaults 的问题/问题

iphone - 如何使用我们的应用程序获取在 iOS 设备中登录的用户详细信息的 instagram 应用程序?

objective-c - CS193p-向计算器添加退格键

android - 关闭网站上密码字段的预测文本

ios - box2D中如何避免碰撞力?

ios - TableView 行中的两个单元格

iphone - 在 CGAffineTransform 之后获取 UIView 的中心