我有一个表格在VC。在表单中有一些字段和文字浏览。当用户点击文本区域键盘弹出窗口,并且用户开始键入它时。但问题在于textview隐藏在键盘后面,用户看不到他/她正在写什么。有没有办法让用户无法解决这个问题?屏幕看起来像这样 调整键盘以显示其背后的内容在IOS
-1
A
回答
0
我建议你在键盘出现时向上移动视图,当键盘消失时向下移动视图。如果你想这样做:
如果你现在的内容不适合在iPhone屏幕上,你将只需要一个ScrollView。 (如果您将ScrollView添加为组件的超级视图,只是为了在键盘弹出时使TextField向上滚动,则不需要。) 为了显示未被键盘隐藏的文本字段,标准方法是移动每当显示键盘时向上/向下浏览具有文本字段的视图。 下面是一些示例代码:
#define kOFFSET_FOR_KEYBOARD 80.0
-(void)keyboardWillShow {
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide {
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)textFieldDidBeginEditing:(UITextField *)sender
{
if ([sender isEqual:mailTf])
{
//move the main view, so that the keyboard does not hide it.
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
}
}
//method to move the view up/down whenever the keyboard is shown/dismissed
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
// 1. move the view's origin up so that the text field that will be hidden come above the keyboard
// 2. increase the size of the view so that the area behind the keyboard is covered up.
rect.origin.y -= kOFFSET_FOR_KEYBOARD;
rect.size.height += kOFFSET_FOR_KEYBOARD;
}
else
{
// revert back to the normal state.
rect.origin.y += kOFFSET_FOR_KEYBOARD;
rect.size.height -= kOFFSET_FOR_KEYBOARD;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
0
把你的形式进入自己的看法,然后只需在键盘出现时调整视图的底部约束/消失。
要启用出现键盘时的通知,以下行添加到viewDidLoad中:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
并实行keyboardWillShow提高键盘鉴于上述约束出现时:
func keyboardWillShow(notification: NSNotification) {
if let info = notification.userInfo {
let rect: CGRect = info["UIKeyboardFrameEndUserInfoKey"] as! CGRect //get the keyboard frame as CGRect to get its dimensions
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.25, animations: {
self.formViewBottomConstraint?.constant = rect.height + 14 //replace 14 with your desired pixel offset from the top of the keyboard
self.view.layoutIfNeeded()
})
}
}
最后,当键盘消失时,将视图的底部约束重置为其原始值:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
self.formViewBottomConstraint?.constant = 14
return true
}
上面的代码只会滑动键盘上方的整个视图,但如果您想上下滑动视图以使每个文本字段位于键盘的正上方(并且会针对每个文本字段进行调整),那么只需设置出现键盘时相对于textField的frame.origin.y的底部约束。
相关问题
- 1. WPF窗体应调整大小以显示其内容
- 2. 在iOS中显示后可以更改键盘类型吗?
- 3. TableView内容高度在键盘显示后更改
- 4. 键盘不显示(IOS)
- 5. iOS键盘显示SAVE
- 6. iOS - 调整UIWebView以适应内容?
- 7. 在键盘背后移动内容不起作用,Swift
- 8. Listview没有显示任何内容按了键盘后退键之后
- 9. SDL Window仅显示其后的内容
- 10. 调整UIView以适合其内容
- 11. 如何设置Java applet透明度以显示其背后的内容?
- 12. 如何在键盘显示屏上调整AlertDialog的大小
- 13. 如何调整我的内容视图,使其不被键盘遮挡?
- 14. Cordova/Phonegap IOS键盘滚动问题,显示白色背景
- 15. AutocompleteTextView在显示键盘时没有显示完整的建议?
- 16. 布局在键盘显示时得到调整
- 17. 设置UIWebView内容在键盘显示时不会移动
- 18. Android的调整布局的一部分时,软键盘显示
- 19. 推键盘高然后调整潘
- 20. 如何手动显示ios键盘?
- 21. 在gcloud计算磁盘调整大小后调整gkePersistentDisk容器卷的容量
- 22. 显示键盘
- 23. 自定义键盘不显示自定义键盘iOS 8
- 24. 当键盘显示和隐藏时调整tableview的大小
- 25. 布局调整错误的,当键盘显示
- 26. 当显示iphone的键盘时调整UIView,如何?
- 27. 在Windows上调整键盘快捷键
- 28. Android键盘开启时,保持背景图像的完整和移动内容
- 29. 显示与InputMethodManager手动软键盘不调整窗口
- 30. 如何调整布局时,键盘显示
使用TPKeyboardAvoiding库。 – KKRocks
我有同样的问题。让你查看可滚动,这样你可以滚动你的视图,直到textView可见。这就是我解决问题的方法。干杯! –