【问题标题】:How to make an UITextView scrollable when the keyboard appears?键盘出现时如何使 UITextView 可滚动?
【发布时间】:2012-12-04 11:05:36
【问题描述】:

我想知道如何在编辑UITextView 时使其可滚动?当用户想要编辑它时,键盘会显示,但UITextView 不再可滚动。所以键盘后面的所有文字都是不可见的。

【问题讨论】:

  • 更改滚动视图的大小,使其不再位于键盘后面。

标签: iphone objective-c keyboard scroll uitextview


【解决方案1】:

你可以缩小你在键盘上出现的 textView 的大小

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    CGRect frame = txtAddNote.frame;
    frame.size.height = 150; //Decrease your textView height at this time
    txtAddNote.frame = frame;
}
-(IBAction)DoneBarBtnPress:(id)sender
{
    CGRect frame = txtAddNote.frame;
    frame.size.height = 212; //Original Size of textView
    txtAddNote.frame = frame;

    //Keyboard dismiss 
    [self.view endEditing:YES];
}

【讨论】:

    【解决方案2】:

    UITextView 开始像这样编辑时滚动视图..

    -(void)textViewDidBeginEditing:(UITextView *)textView
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.3];
        self.view.frame = CGRectMake(self.view.frame.origin.x, -160, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    
    }
    

    在 endEditing 中只需设置默认屏幕,如下所示..

    -(void)textViewDidEndEditing:(UITextView *)textView
    {
        [textView resignFirstResponder];
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3];
        self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
    

    更新

    根据您的要求,我们使用我们的视图设置框架,请参见下面的代码

    -(void)textViewDidBeginEditing:(UITextView *)textView
        {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.3];
             [yourTextView setFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y,self.view.‌​frame.size.width,self.view.frame.size.height - 160)];
            [UIView commitAnimations];
    
        }
    
    -(void)textViewDidEndEditing:(UITextView *)textView
        {
            [textView resignFirstResponder];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.3];
            [yourTextView setFrame:self.view];
            [UIView commitAnimations];
        }
    

    【讨论】:

    • 不,这根本不起作用。我在顶部有一个带有“后退”按钮和“完成”按钮的工具栏。然后我有一个几乎和视图本身一样大的 UITextView。所以当我做你的方法时,视图只是向上移动,然后工具栏就不再可用了。最后,这不会使我的 UITextView 可滚动,而我在 IB 中选择了可滚动选项,并且当我不编辑它时我可以滚动它。
    • @frénésie 只需添加这一行并测试它.. [yourTextView setScrollsToTop:YES];
    • 还有 [textView setScrollEnabled:YES];试试这个
    • 好的,所以当显示键盘时,我的 UITextView 是可滚动的。我想做的是当文本开始在键盘后面时滚动这个 UITextView 。然后只要用户输入越来越多的文本就一直滚动。
    • 它会自动转到您输入文本的位置,如果您想查看您输入的文本,那么在编辑模式下只需 setFrame of textview 并在结束编辑时设置默认值,这是一个简单的想法它..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 2011-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多