我有一个高度为500的UIScrollView,UITextFileds位置为0,100,200,300,400,500(1个UITextFileds,UITextFileds 2,3个UITextFileds,UITextFileds aUITextFileds 4和5)。
ContentOffset位于(0,70)位置。我点击了UITextField 4,我怎么知道TextField 4 contentOffset的位置?
更新
我点击了UITextField 4,我怎么知道我必须上Y像素才能让UITextField不被键盘隐藏?
答案 0 :(得分:1)
我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void) viewWillAppear: (BOOL) animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
}
- (void)setScrollView:(UIScrollView *)scrollView offsetToView:(UIView *)view
{
CGRect contentFrame = CGRectMake(view.frame.origin.x, view.frame.origin.y,view.frame.size.width, view.frame.size.height);
CGPoint contentOffset = CGPointMake(0, contentFrame.origin.y);
scrollView.contentOffset = contentOffset;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self setScrollView:_scrolviewKeys offsetToView:textField];
}
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
_scrolviewKeys.frame = (CGRect){
_scrolviewKeys.frame.origin,(CGSize){
_scrolviewKeys.frame.size.width,_scrolviewKeys.frame.size.height-kbSize.height}
};
}
点击UITextView黄色
结果:
答案 1 :(得分:0)
应该如此简单:
- (void)setScrollView:(UIScrollView *)scrollView bottomOffsetToView:(UIView *)view
{
scrollView.contentOffset = CGPointMake(0, view.frame.origin.y + view.frame.size.height - 216);
}
然后在您的代码中将自己设置为UITextFieldDelegate并实现:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
self.currentTextField = textField;
}
更新:
你在这里问两个不同的东西..对于键盘通知的键盘寄存器(例如在viewWillAppear
中):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
然后根据键盘大小更改滚动视图框架:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
self.scrollView.frame = (CGRect){self.scrollView.frame.origin,(CGSize){self.scrollView.frame.size.width,self.scrollView.frame.size.height-kbSize.height);
if (self.currentTextField != nil)
{
[self setScrollView:self.scrollView bottomOffsetToView:self.currentTextField];
}
}
与KeyboardWasHidden相同的行为(重置初始帧)并且不要忘记在viewDidDisappear中删除Observer。
答案 2 :(得分:0)
在iOS 7中,scrollView将自动滚动最小量以保持textField可见。如果你使用的是iOS 6,那么你可以做的就是模仿同样的行为。
假设您在键盘显示如下后调整了scrollView.contentInset
,这就是我在- (void)textFieldDidBeginEditing:(UITextField *)textField
中所做的:
self.scrollView.contentInset = (UIEdgeInsets){.bottom = 216.0f};
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;
CGFloat minY = CGRectGetMinY([self.view convertRect:textField.frame fromView:textField]);
if (minY > self.scrollView.contentInset.bottom) {
CGFloat visibleHeight = CGRectGetHeight(self.scrollView.bounds) - self.scrollView.contentInset.bottom;
[self.scrollView setContentOffset:CGPointMake(0.0f, CGRectGetMaxY([self.view convertRect:textField.frame fromView:textField]) - visibleHeight) animated:YES];
}
我们首先在参考scrollView的框架时计算textField的框架的minY,然后查看它是否大于我们插入scrollView的数量。如果是,那意味着scrollView的这一部分现在在键盘下面。然后我们通过考虑visibleHeight
来计算偏移量,这是显示键盘后scrollView的可见内容。取textField的框架的maxY并减去visibleHeight以使其与键盘的顶部对齐,从而将scrollView移动到最小量以使其保持可见。