0

I have 3 textfields, but the keyboard covers the textfield when it pops up on each of them. How can I move the textfield up each time the keyboard pops up?

Current Attempt:

The current attempt with the code below gets called, but does not change the layout at all (followed a different StackOverflow answer).

Code Excerpt:

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")

    }

}

This is what the UIViewController looks like:

enter image description here

Onichan
  • 4,476
  • 6
  • 31
  • 60
  • possible duplicate of [Move a view up only when the keyboard covers an input field](http://stackoverflow.com/questions/28813339/move-a-view-up-only-when-the-keyboard-covers-an-input-field) – Nerkyator May 28 '15 at 13:30
  • both textfield hiding ? – Ashish Kakkad May 28 '15 at 13:31
  • @nerkyator can you explain how to apply the answer to multiple textfields? – Onichan May 28 '15 at 13:33
  • @AshishKakkad yes, each one is being covered when the keyboard pops up – Onichan May 28 '15 at 13:33
  • With `activeField` variable. on `textFieldDidBeginEditing` you assign current editing textfield, so when `keyboardWasShown` fires it will scroll the scrollview and keyboard will not cover it. – Nerkyator May 28 '15 at 14:01

3 Answers3

0

You can use IQKeyboardManager in your project from here.

To use IQKeyboardManager you simply need to add source files to your project.

Key Features

  1. CODELESS, Zero Line Of Code

  2. Works Automatically

  3. No More UIScrollView

  4. No More Subclasses

  5. No More Manual Work

  6. No More #imports

Try it!

Kampai
  • 22,848
  • 21
  • 95
  • 95
iYoung
  • 3,596
  • 3
  • 32
  • 59
  • I will give it a try. But I had some issues with third party libs (tried 1-2 already). – Onichan May 28 '15 at 13:34
  • @Emma, but this will not make any difference, you just have to drag & drop the files in your project nothing else. If still facing any issue, do tell me. – iYoung May 28 '15 at 13:35
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – bgilham May 28 '15 at 17:35
  • @bgilham: Thanks for pointing it out! Will remember it in future. :) – iYoung May 29 '15 at 05:43
  • @RajatDeepSingh Hi, i just gave this a try today. But after adding the files to my project, the textfields are still exactly the same (covered by keyboard). – Onichan Jun 02 '15 at 11:46
  • Can you tell me what files you have added? – iYoung Jun 02 '15 at 11:55
  • @RajatDeepSingh actually i was able to get it to work by adding this to the appdelegate `IQKeyboardManager.sharedManager().enable = true`. But there seem to be conflicts with one of my other viewcontrollers that has a bottom bar textfield. Is it possible to disable the IQKeyboardManager on certain viewControllers (swift)? – Onichan Jun 02 '15 at 11:58
  • @RajatDeepSingh even when I set `IQKeyboardManager.sharedManager().disableInViewControllerClass(CommentsViewController)` there are still conflicts with my other viewController see: http://stackoverflow.com/questions/30247506/gesture-recognizer-not-called – Onichan Jun 02 '15 at 12:13
  • @RajatDeepSingh how can I disable it completely in certain viewcontrollers? – Onichan Jun 02 '15 at 12:39
  • You want to use it in only some view Controllers? – iYoung Jun 02 '15 at 12:46
  • You want to use this functionality in VC1 and not in VC2, you are saying this only? – iYoung Jun 02 '15 at 12:47
  • @RajatDeepSingh yes that is correct (because there are conflicts in VC2) – Onichan Jun 02 '15 at 12:48
  • @Emma That's Weird, that worked for me perfectly in all my view controllers, as it automatically detects the textfield views & it automatically adjusts it. – iYoung Jun 02 '15 at 12:59
  • @Emma Just to confirm have you added files of `IQKeyboardManagerSwift` in your code? – iYoung Jun 02 '15 at 13:06
  • @Emma And you are following the set of requirements i.e. Minimum iOS Target: iOS 8.0 Minimum Xcode Version: Xcode 6.3 – iYoung Jun 02 '15 at 13:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79426/discussion-between-emma-and-rajat-deep-singh). – Onichan Jun 02 '15 at 13:45
0

You may get help from TPKeyboardAvoiding sample to solve this issue. Please have a look here https://github.com/michaeltyson/TPKeyboardAvoiding

Sk.Azad
  • 1,900
  • 2
  • 16
  • 20
0

You are almost there.

In your keyboardWillShow: method you need to set the contentInset and the scrollIndicatorInsets

Example:

- (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;
    }];
}
egarlock
  • 559
  • 3
  • 11
  • Thanks. I gave this a try, but my viewcontroller does not have `contentInset` and `scrollIndicatorInsets`. How should they be defined? – Onichan May 28 '15 at 14:24
  • This would work on a `UIScrollView`. I have written the code above in a previous `UITableView` implementation. – egarlock May 28 '15 at 14:32