2013-10-04 150 views
0

我在UiscrollView中出现键盘问题。当键盘显示滚动视图时,textfield消失

我增加一个UIScrollView作为

scrlView=[[UIScrollView alloc] initWithFrame:CGRectMake(10, 140, 1000, 600)]; 
scrlView.scrollEnabled=YES; 
scrlView.showsVerticalScrollIndicator=YES; 
scrlView.bounces=NO; 

该滚动视图我已经加入10行UITextFields的每一行有5个文本框的每个文本框的高度是50像素。 当曾经试图编辑文本字段它是由keyBoard.For重叠,我试图验证码

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


    - (void)keyboardWasShown:(NSNotification*)aNotification 
    { 
NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
CGRect bkgndRect = selectetTxtfld.superview.frame; 
bkgndRect.size.height += kbSize.height; 
[selectetTxtfld.superview setFrame:bkgndRect]; 
[scrlView setContentOffset:CGPointMake(0.0, selectetTxtfld.frame.origin.y) animated:YES]; 
} 

}

//调用时UIKeyboardWillHideNotification发送

- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
    { 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 

    [UIView animateWithDuration:0.4 animations:^{ 
    scrlView.contentInset = contentInsets; 
    }]; 
    scrlView.scrollIndicatorInsets = contentInsets; 
} 

但文本框不出现在keyboard.it出现在scrollview ypoint的位置

帮助我解决这个问题。我看到很多答案StackOverFlow.But未清除我的问题

回答

1
- (void)keyboardWillBeHidden:(NSNotification*)aNotification { 

NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
CGFloat offSetAfterKeyboardIsDisplayed = scrlview.contentOffset.y + kbSize.height; 

[UIView animateWithDuration:0.3 animations:^{ 
//adding content inset at the bottom of the scrollview 
    scrlView.contentInset = UIEdgeInsetMake(0,0,kbSize.height,0); 
    [scrlview setContentOffset:offSetAfterKeyboardIsDisplayed] 
}]; 
} 


- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 

NSDictionary* info = [aNotification userInfo]; 
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
CGFloat offSetAfterKeyboardResigns = scrlview.contentOffset.y - kbSize.height; 

[UIView animateWithDuration:0.3 animations:^{ 
    scrlView.contentInset = UIEdgeInsetsZero; 
    [scrlview setContentOffset:offSetAfterKeyboardResigns] 
}]; 
} 
+0

在scrlView.contentInset显示错误= UIEdgeInsetMake(0,0,kbSize.height,0);指定'UIEdgeInsets'(又名'struct UIEdgeInsets')不兼容类型'int' – sudheer

+0

在[scrlView setContentOffset:offSetAfterKeyboardIsDisplayed]中显示错误将'const CGFloat'(又名'const float')发送到不兼容类型'CGPoint' 'struct CGPoint') – sudheer

+0

tnks for answers again.It现在对我非常重要 – sudheer

1

in keyboardWasShown: 1.在scrollview的底部添加内容插入,其值等于键盘的高度。 2. setContentOffset =当前偏移+键盘的高度。 注:1 & 3应与持续时间的动画块来完成equalto 0.30

在keyboardWillBeHidden: 1.设置contentInset = UIEdgeInsetsZero 2. setContentOffset =当前偏移 - 键盘的高度。 注:1 & 3应具有持续时间的一个动画块进行equalto 0.30

这应该解决乌尔问题:)

+0

谢谢回答.PLS你可以用一些样本code.I上午介绍它new to xcode deve – sudheer

相关问题