我正在使用IOS Developer Library中的示例来管理键盘。 Text, Web and Edition Programming Guide
我使用IB创建了一个视图。它是一个简单的UI,具有UIScrollView,UITextView,UIButton和UITextField。我将UIScrollView放在我的视图上,然后将所有其他控件添加为此scrollview的子项。 scrollview通过IBOutlet“scrollView”公开给viewcontroller。
执行跟随代码时,用户将焦点设置为textField,但我从未看到滚动条出现且滚动条的内容未移动。我应该能够默认看到滚动条吗?有人能告诉我我错过了什么吗?
-(void) keyboardWasShown:(NSNotification *)aNotification{
NSDictionary * info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
同样,我直接从链接中的IOS编程指南中获取此代码。 UI布局看起来像一个基本的聊天窗口。我想在软键盘可见的同时向上移动“输入”字段。谢谢! 的更新
似乎我需要添加一些填充来实际看到位于scrollview底部的控件。
CGPoint scrollPoint = CGPointMake(0.0, (activeField.frame.origin.y - kbSize.height) + 10.0);
为什么我看不到滚动条?
答案 0 :(得分:1)
如果您将父母视为滚动,那么只需使用:UITextFielddelegate并设置方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField == self.txtUserName)
{
[self.txtPassword becomeFirstResponder];
}
else if(textField == self.txtPassword){
[self.txtPassword resignFirstResponder];
CGPoint bottomOffset = CGPointMake(0, 0);
[scrView setContentOffset:bottomOffset animated:YES];
[textField resignFirstResponder];
}
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField == self.txtUserName)
{
CGPoint bottomOffset = CGPointMake(0, 80);
[scrView setContentOffset:bottomOffset animated:YES];
}
if (textField == self.txtPassword)
{
CGPoint bottomOffset = CGPointMake(0, 135);
[scrView setContentOffset:bottomOffset animated:YES];
}
}
答案 1 :(得分:0)
滚动条应仅显示在用户互动上。由于您以编程方式设置了滚动视图的插图,因此不是这种情况。
如果你想显示滚动条,我相信UIScrollView
定义了一个flashScrollIndicators
方法,它应该显示滚动条。