UITextView隐藏键盘

时间:2011-05-30 05:36:42

标签: ios iphone keyboard uitextview

我正在尝试将带照片的信息发送到evernote。我为此创建了视图

首先我将UITextField用于标题 然后我为UITextView添加了消息,并给出了一个附加照片的按钮,并有一个图像视图来保存来自UIImagePicker的图像。当我在UITextView键盘上写一些文字时,会覆盖附加照片按钮和图像按钮。如何隐藏键盘......或者我可以最小化键盘的高度

5 个答案:

答案 0 :(得分:1)

无需最小化您可以向上滑动视图的高度,并且在输入文本后需要隐藏键盘,为此,

隐藏键盘 - 在键盘上方添加工具栏或在导航栏中添加栏按钮。并点击按钮

[youTextView resignFirstResponder];

用于添加工具栏,在视图下方制作工具栏,并在点击文本时查看委托将在此处调用textViewShouldBeganEditing这样的内容

 CGRect tlFrame=yourToolBar.frame;
  tlFrame.origin.y-=40;  //check this according to you
  yourToolBar.frame=tlFrame.origin.y;

还会添加一些动画,并在隐藏键盘时创建它的原点。

答案 1 :(得分:0)

如果隐藏键盘,那么您希望用户如何输入消息?您可以自定义键盘以更改其高度。一种理想的方法是向上移动视图,以便在键盘出现时,按钮和图像仍然可见。

答案 2 :(得分:0)

保留背景按钮,当用户完成输入文本视图消息时,您可以期望用户点击外面的某个位置,以便此背景按钮获取点击事件并取消键盘。

如下:

-(IBAction) backgroundButtonPressed:(id) sender {
    [self.textView resignFirstResponder];
}

答案 3 :(得分:0)

为此,您可以将视图设置为动画..

CGFloat animatedDistance;   // in.h file

并在.m文件中执行以下操作

#pragma mark (Text View Delegate Method)
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


- (void)textViewDidBeginEditing:(UITextView *)textView
{

    static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
    static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
    static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
    static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
    static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;

    CGRect textFieldRect;
    CGRect viewRect;

    textFieldRect =[self.view.window convertRect:textView.bounds fromView:textView];
    viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];


    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame;

    viewFrame= self.view.frame;
    viewFrame.origin.y -= animatedDistance;


    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];

}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.tag==0)
    {
        static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
        CGRect viewFrame;

        viewFrame= self.view.frame;
        viewFrame.origin.y += animatedDistance;

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationBeginsFromCurrentState:YES];
        [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];


        [self.view setFrame:viewFrame];
        [UIView commitAnimations];



    }
}

这将解决您的问题,以便不会隐藏任何内容

答案 4 :(得分:0)

在完成按钮中调用此方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
   [textField resignFirstResponder];
   return NO;
}
相关问题