0

在我的应用程序中,我在视图的顶部和底部包含标题和横幅图像,还包括一个带有更多 15 个 UITextfields 的滚动视图。

我使用了anyW,anyH自动布局,父视图的高度设置为自由格式,高度为2500。

我使用下面的代码在屏幕上隐藏和显示键盘,它工作正常,还使用下面的代码将活动文本字段移动到键盘上方。

我在这里面临的问题是当前的活动文本文件根据视图的自由形式高度采用了错误的 x、y 位置。

下面是 Firstname 文本字段的 x,y 位置,但它实际上放置在屏幕顶部,框架为 a(100 250; 270 28);

UITextField:0x1918a820;帧 = (14.5 1405; 270 28); 文本 = ''; clipsToBounds = YES; 不透明=否;自动调整大小 = RM+BM;手势识别器 = NSArray: 0x191a5900; 层=CALayer:0x19189f40

当我开始在名字字段中输入文本时,它将名字字段框架位置设为 (14.5 1405; 270 28) 并像第二个屏幕一样向上滚动位置。

在此处输入图像描述

现在它位于 Y 位置 1415。为什么我的活动文本字段采用错误的 x、y 位置?

在此处输入图像描述

为什么我的活动文本字段采用错误的帧位置并解决此问题?

- (void) viewWillAppear:(BOOL)animated 
{
    [super viewWillAppear:animated];

    NSLog(@"Registering for keyboard events");

    // Register for the events

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];

    // Setup content size
    _scrollViewAddContact.contentSize = CGSizeMake(self.view.frame.size.width,_scrollViewAddContact.frame.size.height);
//Initially the keyboard is hidden

    keyboardVisible = NO;

}

-(void) viewWillDisappear:(BOOL)animated {

    NSLog (@"Unregister for keyboard events");

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}

-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {

      activeField = textField;
    return YES;

}


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

    return YES;
}

-(void) keyboardDidShow: (NSNotification *)notif {

    NSLog(@"Keyboard is visible");

    // If keyboard is visible, return

    if (keyboardVisible) {

        NSLog(@"Keyboard is already visible. Ignore notification.");

        return;

    }

    // Get the size of the keyboard.

    NSDictionary* info = [notif userInfo];

    NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGSize keyboardSize = [aValue CGRectValue].size;


    // Save the current location so we can restore

    // when keyboard is dismissed

    offset = _scrollViewAddContact.contentOffset;


    // Resize the scroll view to make room for the keyboard

    CGRect viewFrame = _scrollViewAddContact.frame;

    viewFrame.size.height -= keyboardSize.height;

    _scrollViewAddContact.frame = viewFrame;


    CGRect textFieldRect = [activeField frame];

    textFieldRect.origin.y += 10;

    [_scrollViewAddContact scrollRectToVisible:textFieldRect animated:YES];


    NSLog(@"ao fim");

    // Keyboard is now visible

    keyboardVisible = YES;

}

-(void) keyboardDidHide: (NSNotification *)notif {

    // Is the keyboard already shown

    if (!keyboardVisible) {

        NSLog(@"Keyboard is already hidden. Ignore notification.");

        return;

    }

    // Reset the frame scroll view to its original value

    _scrollViewAddContact.frame = CGRectMake(_scrollViewAddContact.frame.origin.x, _scrollViewAddContact.frame.origin.y, self.view.frame.size.width, _scrollViewAddContact.frame.size.height);
     // Reset the scrollview to previous location

    _scrollViewAddContact.contentOffset = offset;

     // Keyboard is no longer visible

    keyboardVisible = NO;

}
4

0 回答 0