0

I am implementing an app where tapping a button will pop up a location entry screen which is implemented with Modally presented Segue and Visual Effect with Blur to make the entry window appear hovering above the main screen.

The pop up screen has a text field for location name and a multi line text view for remarks.

Problem is the multi line text view is partially obscured by the keyboard on smaller devices and in landscape view.

Landscape View

Portrait View

Modal Segue View

I tried to utiize the solution described here, which is supposed to utilize scroll view to shift the view when keyboard appears, but it doesn't seem to work for my project: Move a view up only when the keyboard covers an input field

This question doesn't answer my problem as my text field and view are inside another view to make it appear like a popup to the main screen: Move a view up only when the keyboard covers an input field

In order to implement the pop up view (in a modal segue) I placed the text field and view inside a View which is placed inside a Scroll View which is located inside the view of the Visual Effects with Blur View.

Another problem I'm experiencing is I impleneted the following code to dismiss keyboard when tapping outside the text field/view, but for some reason it doesn't seem to work

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
    self.view.endEditing(true)

}

Here is the code for the View Controller of the pop up view in Xcode 9.2 Archive of the project: Xcode Project Archive

//
//  LocationEntryScrollViewController.swift
//

import UIKit

class LocationEntryScrollViewController: UIViewController, UITextViewDelegate, UITextFieldDelegate {

    @IBOutlet weak var ScrollView: UIScrollView!
    @IBOutlet weak var LocationEntryView: UIView!
    @IBOutlet weak var LocationTextField: UITextField!
    @IBOutlet weak var RemarksTextView: UITextView!

    var activeTextField: UITextField?
    var activeTextView: UITextView?

    override func viewDidLoad()
    {
        super.viewDidLoad()
        registerForKeyboardNotifications()

        LocationEntryView.layer.cornerRadius = 10
        LocationEntryView.layer.masksToBounds = true

        LocationTextField.delegate = self   // dismiss keyboard when tapped outside keyboard
        RemarksTextView.delegate = self   // dismiss keyboard when tapped outside keyboard
    }

    override func viewDidAppear(_ animated: Bool)
    {
        super.viewDidAppear(animated)
        LocationTextField.becomeFirstResponder()
        RemarksTextView.becomeFirstResponder()
    }

    /**
     * Called when the user click on the view (outside the UITextField).
     */
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        self.view.endEditing(true)

    }

    override func viewDidDisappear(_ animated: Bool)
    {
        deregisterFromKeyboardNotifications()
    }

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

    @IBAction func DoneButtonTapped(_ sender: Any)
    {
        dismiss(animated: true, completion: nil)
    }

    @IBAction func CancelButtonTapped(_ sender: Any)
    {
    }

    @objc func keyboardWasShown(notification: NSNotification)
    {
        //Need to calculate keyboard exact size due to Apple suggestions
        self.ScrollView.isScrollEnabled = true
        var info = notification.userInfo!
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)

        self.ScrollView.contentInset = contentInsets
        self.ScrollView.scrollIndicatorInsets = contentInsets

        var aRect : CGRect = self.view.frame
        aRect.size.height -= keyboardSize!.height
        if let activeTextField = self.activeTextField
        {
            if (!aRect.contains(activeTextField.frame.origin))
            {
                self.ScrollView.scrollRectToVisible(activeTextField.frame, animated: true)
            }
        }

        if let activeTextView = self.activeTextView
        {
            if (!aRect.contains(activeTextView.frame.origin))
            {
                self.ScrollView.scrollRectToVisible(activeTextView.frame, animated: true)
            }
        }
    }

    @objc func keyboardWillBeHidden(notification: NSNotification)
    {
        //Once keyboard disappears, restore original positions
        let info : NSDictionary = notification.userInfo! as NSDictionary
        let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
        let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
        self.ScrollView.contentInset = contentInsets
        self.ScrollView.scrollIndicatorInsets = contentInsets
        self.view.endEditing(true)
        self.ScrollView.isScrollEnabled = false

    }

    func textFieldDidBeginEditing(_ textField: UITextField)
    {
        activeTextField = textField
    }


    func textFieldDidEndEditing(_ textField: UITextField)
    {
        activeTextField = nil
    }

    func textViewDidBeginEditing(_ textView: UITextView)
    {
        activeTextView = textView
    }

    func textViewDidEndEditing(_ textView: UITextView) {
        activeTextView = nil
    }

    func registerForKeyboardNotifications()
    {
        //Adding notifies on keyboard appearing
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

    func deregisterFromKeyboardNotifications()
    {
        //Removing notifies on keyboard appearing
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
}
Guy Fisher
  • 31
  • 4
  • Possible duplicate of [Move a view up only when the keyboard covers an input field](https://stackoverflow.com/questions/28813339/move-a-view-up-only-when-the-keyboard-covers-an-input-field) – Dan Karbayev Mar 12 '18 at 22:55
  • No , my case is more complicated as I have the text fields inside a small View which is inside the Scroll View, I tried that solution (as I explained in my questions) and it did not work! – Guy Fisher Mar 13 '18 at 22:31

0 Answers0