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

时间:2015-05-28 13:23:33

标签: ios objective-c swift

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

当前尝试:

调用当前使用下面代码的尝试,但根本不会更改布局(遵循不同的StackOverflow答案)。

代码摘录:

class UploadPhotoViewController: UIViewController, UIImagePickerControllerDelegate, UITextFieldDelegate, UIActionSheetDelegate {

    @IBOutlet weak var photoTitleTextField: UITextField! // title textfield
    @IBOutlet var photoCommentTextField: UITextField! 
    @IBOutlet var photoPriceTextField: UITextField!


    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)


        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("keyboardWillShow:"),
            name: UIKeyboardWillShowNotification,
            object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self,
            selector: Selector("keyboardWillHide:"),
            name: UIKeyboardWillHideNotification,
            object: nil)
    }

    override func viewWillDisappear(animated: Bool) {
        NSNotificationCenter.defaultCenter().removeObserver(self)

        super.viewWillDisappear(animated)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        self.photoTitleTextField.delegate = self;
        self.photoCommentTextField.delegate = self;
        self.photoPriceTextField.delegate = self;

        }
    }

    func goBack()
    {
        dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.view.endEditing(true);
        return false;
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        self.view.endEditing(true);
    }

    func keyboardWillShow(notification: NSNotification) {

        println("will show")

        var info = notification.userInfo!
        var keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.bottomConstraint.constant = keyboardFrame.size.height + 20
        })


    }

    func keyboardWillHide(notification: NSNotification) {
        println("will hide")

    }

}

这就是UIViewController的样子:

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以在here的项目中使用IQKeyboardManager。

  

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

主要功能

  1. CODELESS ,零行代码

  2. 自动投放

  3. 没有更多UIScrollView

  4. 没有更多的子类

  5. 不再需要手动工作

  6. 没有更多#imports

  7. 试试吧!

答案 1 :(得分:0)

您可以从TPKeyboardAvoiding示例获得帮助以解决此问题。请看这里https://github.com/michaeltyson/TPKeyboardAvoiding

答案 2 :(得分:0)

你快到了。

keyboardWillShow:方法中,您需要设置contentInsetscrollIndicatorInsets

示例:

- (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;
    }];
}