Iphone 文本字段与UIView键盘上的文本重叠

Iphone 文本字段与UIView键盘上的文本重叠,iphone,ios,objective-c,Iphone,Ios,Objective C,我正在使用iOS应用程序,在其中一个视图中,我必须加载用户评论并让他们发表评论。一切看起来都很好,直到键盘出现在屏幕上,用户点击文本字段输入评论。UIView上的文本与textfield重叠,如下所示 感谢您查看。假设用户点击文本字段时UITextField和UIButtonPost向上移动,您应该同时将其上方的所有内容向上移动相等距离。假设用户点击文本字段时UITextField和UIButtonPost向上移动,您应该将其上方的所有内容向上移动同时具有相等的距离。在iOS中,通常将启用键盘的

我正在使用iOS应用程序,在其中一个视图中,我必须加载用户评论并让他们发表评论。一切看起来都很好,直到键盘出现在屏幕上,用户点击文本字段输入评论。UIView上的文本与textfield重叠,如下所示


感谢您查看。

假设用户点击文本字段时UITextField和UIButtonPost向上移动,您应该同时将其上方的所有内容向上移动相等距离。

假设用户点击文本字段时UITextField和UIButtonPost向上移动,您应该将其上方的所有内容向上移动同时具有相等的距离。

在iOS中,通常将启用键盘的视图中的所有视图元素添加到UIScrollView中。这样,当键盘向上滑动时,其他元素也随之向上滑动,以避免受到阻碍

幸运的是,有人实现了一个UIScrollView,它可以自动避免键盘,所以您只需将视图元素插入其中一个元素,一切都应该工作正常


在iOS中,通常将启用键盘的视图中的所有视图元素添加到UIScrollView中。这样,当键盘向上滑动时,其他元素也随之向上滑动,以避免受到阻碍

幸运的是,有人实现了一个UIScrollView,它可以自动避免键盘,所以您只需将视图元素插入其中一个元素,一切都应该工作正常


由于屏幕上不仅仅只有一点点文本,您必须使用UIScrollView。然后可以使用[self.scrollView setContentOffset:CGPointMake0,offset-animated:YES];要相对于键盘上下移动文本。

如果屏幕上的文本不止一点点,则必须使用UIScrollView。然后可以使用[self.scrollView setContentOffset:CGPointMake0,offset-animated:YES];相对于键盘上下移动文本。

对post按钮和UITextField使用inputAccessoryView。 像这样:

-(void)addAccessoryViewOnTextView
{
    UIToolbar *keyboardHeaderView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 1024, 39)];

    keyboardHeaderView.tintColor = [UIColor blackColor];

    /* Here use your post button and textfield.
    UIBarButtonItem *nextBtn= [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(nextEvent:)];
     UIBarButtonItem *prevBtn= [[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStyleDone target:self action:@selector(prevEvent:)];
    UIBarButtonItem *doneBtn= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneEvent:)];*/

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [keyboardHeaderView setItems:[NSArray arrayWithObjects: doneBtn,flexibleSpace,prevBtn, nextBtn, nil]];

    [yourTextField setInputAccessoryView:keyboardHeaderView];
    keyboardHeaderView = nil;

}
并在viewDidLoad或UITextField初始化的任何位置调用此方法

将inputAccessoryView用于post按钮和UITextField。 像这样:

-(void)addAccessoryViewOnTextView
{
    UIToolbar *keyboardHeaderView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 1024, 39)];

    keyboardHeaderView.tintColor = [UIColor blackColor];

    /* Here use your post button and textfield.
    UIBarButtonItem *nextBtn= [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(nextEvent:)];
     UIBarButtonItem *prevBtn= [[UIBarButtonItem alloc]initWithTitle:@"Previous" style:UIBarButtonItemStyleDone target:self action:@selector(prevEvent:)];
    UIBarButtonItem *doneBtn= [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneEvent:)];*/

    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [keyboardHeaderView setItems:[NSArray arrayWithObjects: doneBtn,flexibleSpace,prevBtn, nextBtn, nil]];

    [yourTextField setInputAccessoryView:keyboardHeaderView];
    keyboardHeaderView = nil;

}
并在viewDidLoad或UITextField初始化的任何位置调用此方法


我目前正在动态构建UILabel,并将它们动态绑定到UISCrollView。然而,TPKeyboardAvoiding似乎负责静态绑定到UIScrollView的事情。这个问题有解决方案吗?不要使用UIScrollView,使用提供的子类。我不确定我是否明白这个问题。问题是之前提到的问题仍然存在。我正在动态地将注释添加到类型为UIScrollView的TPKeyboardAvoiding类中。当我这样做时,如果添加超过4条注释,它将与textfield重叠,textfield是TPKeyboardAvoidingScrollView的一部分。我目前正在动态地构建UIlabels并将它们动态绑定到UIScrollView。然而,TPKeyboardAvoiding似乎负责静态绑定到UIScrollView的事情。这个问题有解决方案吗?不要使用UIScrollView,使用提供的子类。我不确定我是否明白这个问题。问题是之前提到的问题仍然存在。我正在动态地将注释添加到UIScrollView类型的TPKeyboardAvoiding类中。当我这样做时,如果我添加了4条以上的注释,它将与textfield重叠,textfield是TPKeyboardAvoidingScrollView的一部分。