在键盘iOS上方添加UITextField

时间:2015-01-19 10:24:40

标签: ios objective-c keyboard

在我的应用程序中,我正在尝试在accessoryView上添加UITextField。它应该如下工作:

  1. 我在app的用户界面中按下UITextField
  2. 键盘弹出并覆盖用户界面上的UITextField
  3. 要编辑文本,我想在键盘上方的accessoryView中显示UITextField
  4. 我在网上看了一下,但我发现只在附件上添加按钮的东西,我希望你能帮我找到一个在附件上添加UITextField的解决方案。

    谢谢

    更新

    这就是我的意思:

    enter image description here

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

3 个答案:

答案 0 :(得分:1)

in .h

UIView *customtoolbar;

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

答案 1 :(得分:0)

这就是我在我的应用中所做的

  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 commitAnimations]

答案 2 :(得分:0)

您可以在自己的应用中使用滚动视图。设置内容偏移量

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

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

}

我希望这与您的问题相关