如何设置contentoffset并同时更改UIScrollview的高度

时间:2011-03-05 04:12:30

标签: keyboard uiscrollview

我正在尝试在显示键盘时向上滚动UISCrollView

我使用setContentOffset来移动uiview。

同时我想将UISCrollView的高度降低到(查看高度 - 键盘高度),以便可以滚动整个内容视图。

我在keyboardWillShow通知中应用了这两项更改

当我实际将键盘放入我的应用程序时,内容首先被推高然后​​被推下(这会产生闪烁效果)。我试图一次性完成两个转换......

有可能吗?

以下代码---

- (void) keyboardWillShow {
   CGPoint contentOffset = scrollView.contentOffset;

    CGRect scrollViewFrame = scrollView.frame;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];

    if (contentOffset.y >= 0 && contentOffset.x >= 0) {
        isContentOffset = YES;
        contentOffset.y += screenShift;
        [scrollView setContentOffset:contentOffset animated: NO];
    }

    scrollViewFrame.size.height = keyboardOrigin.y - self.view.frame.origin.y - toolbarHeight;
    scrollView.frame = scrollViewFrame;
    [UIView commitAnimations];
}

4 个答案:

答案 0 :(得分:3)

当您为动画设置SetContentOffset时,有一个动画选项。这是我一直使用的代码

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    svos = scrollView.contentOffset;
    CGRect rect = [textView bounds];
    rect = [textView convertRect:rect toView:self.scrollView];
    CGPoint point = rect.origin ;
    point.x = 0 ;
    [self.scrollView setContentOffset:point animated:YES];
    doneButton.enabled = YES;
}

- (IBAction)donePressed
{
    [scrollView setContentOffset:svos animated:YES]; 
    [textview resignFirstResponder];
    doneButton.enabled = NO;
}

它适用于我。

答案 1 :(得分:1)

您是否将这些更改包装在动画块中?

[UIView beginAnimations:@"resizeScroll" context:nil];
// make your changes to set and content offset
[UIView commitAnimations];

答案 2 :(得分:0)

我想我有一个解决方案。您可以使用textViewDidBeginEditing方法处理视图的大小调整。您只需将scrollView的帧大小更改为一半

即可
CGRect tframe = myscrollView.frame;
tframe.size.height/=2;
myscrollView.frame = tframe;

并初始化或处理scrollview的总长度,您可以以与上面代码段中设置的框架类似的方式设置scrollView的contentSize。例如

CGSize tsize = self.view.frame.size;
//here you can change the height and width of the scrollView
myscrollView.contentSize = tsize;

我希望这应该为你做的伎俩。如果确实如此,请进行沟通。

答案 3 :(得分:0)

找出问题...

单击键盘按钮时,我在键盘视图上执行了一个yesFirstResponder,后跟一个resignFirstResponder。这导致keyboardWillShow后跟keyboardWillHide和另一个keyboardWillShow通知,它使键盘上升,然后再将其重新启动。

感谢所有试图提供帮助的人...抱歉,这个问题非常不同......