Ios 键盘覆盖文本字段(多个文本字段)

Ios 键盘覆盖文本字段(多个文本字段),ios,objective-c,swift,Ios,Objective C,Swift,我有3个文本字段,但当每个文本字段上都弹出时,键盘会覆盖文本字段。如何在每次键盘弹出时向上移动文本字段 当前尝试: 调用下面代码的当前尝试,但根本不会更改布局(后面是不同的StackOverflow答案) 代码摘录: 类上载PhotoViewController:UIViewController、UIImagePickerController、UIExtFieldDelegate、UIActionSheetDelegate{ @IBOutlet弱var photoTitleTextField:U

我有3个文本字段,但当每个文本字段上都弹出时,键盘会覆盖文本字段。如何在每次键盘弹出时向上移动文本字段

当前尝试: 调用下面代码的当前尝试,但根本不会更改布局(后面是不同的StackOverflow答案)

代码摘录:
类上载PhotoViewController:UIViewController、UIImagePickerController、UIExtFieldDelegate、UIActionSheetDelegate{
@IBOutlet弱var photoTitleTextField:UITextField!//标题textfield
@IBOutlet var光通信文本字段:UITextField!
@IBOutlet var photoPriceTextField:UITextField!
覆盖功能视图显示(动画:Bool){
super.viewdide显示(动画)
NSNotificationCenter.defaultCenter().addObserver(self,
选择器:选择器(“键盘将显示:”),
名称:UIKeyboardWillShowNotification,
对象:无)
NSNotificationCenter.defaultCenter().addObserver(self,
选择器:选择器(“keyboardWillHide:”),
名称:UIKeyboardWillHideNotification,
对象:无)
}
覆盖功能视图将消失(动画:Bool){
NSNotificationCenter.defaultCenter().removeObserver(自)
超级。视图将消失(动画)
}
重写func viewDidLoad(){
super.viewDidLoad()
self.photoTitleTextField.delegate=self;
self.photoCommentTextField.delegate=self;
self.photoPriceTextField.delegate=self;
}
}
func goBack()
{
dismissViewControllerAnimated(真,完成:无)
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
func textField应返回(textField:UITextField)->Bool{
self.view.endEditing(true);
返回false;
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
self.view.endEditing(true);
}
func键盘将显示(通知:NSNotification){
println(“将显示”)
var info=notification.userInfo!
var keyboardFrame:CGRect=(info[UIKeyboardFrameEndUserInfoKey]as!NSValue).CGRectValue()
animateWithDuration(0.1,动画:{()->Void in
self.bottomConstraint.constant=keyboardFrame.size.height+20
})
}
func键盘将隐藏(通知:NSNotification){
println(“将隐藏”)
}
}
这就是UIViewController的外观:

您可以从在项目中使用IQKeyboardManager

要使用IQKeyboardManager,只需将源文件添加到项目中即可

主要功能

  • 无代码,零行代码

  • 自动工作

  • 没有更多的UIScrollView

  • 没有更多的子类

  • 不用再做体力活了

  • 不再进口


  • 试试看

    您可以从TPKeyboard Avoiding sample获得帮助来解决此问题。请看这里

    你就快到了

    键盘上,将显示:
    方法,您需要设置
    内容插入项和
    滚动指示项集

    例如:

    - (void)keyboardWasShown:(NSNotification *)notification {
        NSDictionary *info = [notification userInfo];
    
        CGREct keyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfo] CGRectValue];
    
        CGFloat bottomInset = keyboardRect.size.height;
    
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, bottomInset, 0.0);
    
        [UIView animateWithDuration:0.5 animations:^{
            self.contentInset = contentInsets;
            self.scrollIndicatorInsets = contentInsets;
        }];
    }
    

    两个文本字段隐藏可能重复?@nerkyator您能解释一下如何将答案应用于多个文本字段吗?@AshishKakkad是的,当键盘弹出
    activeField
    变量时,每个文本字段都被覆盖。在
    textfieldDiBeginediting
    上,您指定当前编辑的textfield,因此当显示
    keyboard
    时,它将滚动滚动视图,键盘将不会覆盖它。是否有快速版本?我将尝试一下。但是我在使用第三方LIB时遇到了一些问题(已经尝试了1-2)。@Emma,但这不会有任何区别,您只需拖放项目中的文件,其他什么都不用做。如果仍然面临任何问题,请告诉我。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,只有链接的答案可能会无效。@B吉尔汉姆:谢谢您指出这一点!以后会记得的。:)@拉贾特迪普辛格嗨,我今天刚刚试了一下。但是在把这些文件添加到我的项目中之后,文本字段仍然完全相同(由键盘覆盖)。谢谢。我尝试了一下,但是我的viewcontroller没有
    contentInset
    ScrollIndicationSet
    。应该如何定义它们?这将在
    UIScrollView
    上工作。我已经在以前的
    UITableView
    实现中编写了上述代码。
    - (void)keyboardWasShown:(NSNotification *)notification {
        NSDictionary *info = [notification userInfo];
    
        CGREct keyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfo] CGRectValue];
    
        CGFloat bottomInset = keyboardRect.size.height;
    
        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, bottomInset, 0.0);
    
        [UIView animateWithDuration:0.5 animations:^{
            self.contentInset = contentInsets;
            self.scrollIndicatorInsets = contentInsets;
        }];
    }