简体   繁体   English

iPhone - 如何在键盘出现时滚动UITextField并调整UITextView的大小

[英]iPhone - How may I scroll a UITextField and resize a UITextView when keybord appears

I have a UIScrollView inside which I have a UITextField and a UITextView. 我有一个UIScrollView里面有一个UITextField和一个UITextView。 I want to prevent the keyboard to hide the field we are working on. 我想阻止键盘隐藏我们正在处理的字段。 If my user tap on the TextField, I want it to scroll to top of the screen. 如果我的用户点击TextField,我希望它滚动到屏幕顶部。 If my user tap on the TextView, I want it to resize to the empty place left on the top of the keyboard. 如果我的用户点击TextView,我希望它调整大小到键盘顶部左侧的空白处。 My keyboard has an accessory view (a toolbar). 我的键盘有一个附件视图(工具栏)。

For now, I catch the UIKeyboardWillShowNotification notification, and I use this code, that does not really work (the textview is resized, but is still behind the keyboard) : This code worked well when I hadn't the UIScrollView. 现在,我抓住了UIKeyboardWillShowNotification通知,并且我使用了这个代码,它实际上不起作用(textview已调整大小,但仍然在键盘后面):当我没有UIScrollView时,这段代码运行良好。

- (void)keyboardWillShow:(NSNotification *)notification {

    if ([self.noteView isFirstResponder] == NO) return;

    /*
     Reduce the size of the text view so that it's not obscured by the keyboard.
     Animate the resize so that it's in sync with the appearance of the keyboard.
     */

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];                            // Get the origin of the keyboard when it's displayed.
    NSValue *animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];   // Get the duration of the animation.

    // Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.view.bounds;
    newTextViewFrame.size.height = keyboardTop - self.view.frame.origin.y; // Using bounds here does not help

    NSTimeInterval animationDuration;
    [animationDurationValue getValue:&animationDuration];

    // Animate the resize of the text view's frame in sync with the keyboard's appearance.
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:animationDuration];

     self.noteView.frame = newTextViewFrame;

    [UIView commitAnimations];
}

So could you help me to : 你能帮助我:

  • make this work for the textview ? 为textview做这个工作?

  • make the Textview go back to its original position ? 让Textview回到原来的位置?

  • How may I do to scroll the TextField without any resizing feature ? 如何在没有任何调整大小功能的情况下滚动TextField?

Thank you 谢谢

Could you just programmatically scroll the uiscrollview using this: 你能用编程方式滚动uiscrollview吗:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

Not sure if this will work for your application or not. 不确定这是否适用于您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在键盘出现时在iOS上调整UITextView的大小? - How to resize UITextView on iOS when a keyboard appears? iPhone:切换视图时如何使 UITextField、UITextView 不可编辑 - iPhone: How to make a UITextField, UITextView uneditable when switching views 如何按方向更改在以编程方式创建的滚动视图中调整以编程方式创建的UILabel,UITextView,UITextfield的大小 - How to resize the programmatically created UILabel,UITextView,UITextfield in programmatically created scroll view as per orientation change 如何在iPhone UITextView或UITextField中更改书写方向 - How to change the writing direction in iPhone UITextView or UITextField iPhone-我怎么知道scrollToRowAtIndexPath是否可以滚动某些内容 - iPhone - How may I know if scrollToRowAtIndexPath will scroll something 放大iPhone SDK时如何调整滚动视图的子视图大小 - How to resize child view of a scroll view when zooming in iphone sdk UITextView在iPhone中滚动(自动)? - UITextView scroll (Automatically) in iPhone? 当键盘可见时,如何让UITextView正确滚动 - How do I get UITextView to scroll properly when the keyboard is visible 当键盘出现时,如何使UITextView可滚动? - How to make an UITextView scrollable when the keyboard appears? 使用iPhone时网站上会出现水平滚动 - Horizontal scroll appears on website when using iPhone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM