在键盘上方添加 UITextField iOS

Add UITextField above keyboard iOS

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

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

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

谢谢

更新

这就是我的意思:

我希望你能更好地理解我的问题,我不是在寻找应用户滚动视图或其他东西的解决方案,我不想更改我的应用程序的界面...

这是我在我的应用程序中所做的

  1. 将观察者添加到 UIKeyboardWillHideNotification & UIKeyboardWillShowNotification

  2. 当您查看通知时调整您的视图大小keyboardFrame

    double duration = [[notification userInfo][UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    int curve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] intValue];
    
    NSValue *value = [notification userInfo][UIKeyboardFrameEndUserInfoKey];
    CGRect rawFrame      = [value CGRectValue];
    CGRect keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    

    // 调整视图大小

    [UIView 提交动画]

您可以在您的应用中为此使用滚动视图并设置内容偏移量

- (void)textFieldDidBeginEditing:(UITextField *)textField {

self.scroll.contentOffset = CGPointMake(0, textField.frame.origin.y);

}

我希望这与您的问题有关

添加一个视图,然后在上面放一个文本框试试。下面是代码。

UIView * container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
[container setBackgroundColor:[UIColor redColor]];
UITextField * newTextfield = [[UITextField alloc] initWithFrame:CGRectMake(50, 0, 100, 44)];
[newTextfield setBackgroundColor:[UIColor yellowColor]];
[container addSubview:newTextfield];


self.textField.inputAccessoryView = container;

这里包含一些颜色,只是为了区分和识别视图。根据您的需要更改和格式化。它会起作用。 :)

在.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];
}