我在vc中有一个表单。在表单中有一些字段和文本视图。当用户单击文本区键盘弹出时,用户开始在其上键入。但问题是文本视图隐藏在键盘后面,用户无法看到他/她正在写什么。有没有办法让用户无法解决这个问题?屏幕看起来像这样
答案 0 :(得分: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];
}
答案 1 :(得分:0)
将表单放入自己的视图中,然后在键盘出现/消失时调整View的底部约束。
要启用键盘出现的通知,请将以下行添加到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的底部约束。