Showing posts with label keyboard. Show all posts
Showing posts with label keyboard. Show all posts

Friday, October 28, 2016

Adding Done Button to Keyboard

In this example I use a UIToolBar to contain the done button but you can use any UIView to assign to inputAccessoryView.
 
class ViewController: UIViewController {

    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var phoneNumberTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let keyboardToolBar = UIToolbar()
        keyboardToolBar.sizeToFit()
        
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: 
            UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let doneButton = UIBarButtonItem(barButtonSystemItem: 
            UIBarButtonSystemItem.done, target: self, action: #selector(self.doneClicked) )
        
        keyboardToolBar.setItems([flexibleSpace, doneButton], animated: true)
        
        nameTextField.inputAccessoryView = keyboardToolBar
        phoneNumberTextField.inputAccessoryView = keyboardToolBar
    }

    func doneClicked() {
        view.endEditing(true)
    }
}
 

Here is an example of using a Navigation Bar:
 
let navItem = UINavigationItem()
let doneBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, 
    target: self, action: #selector(self.doneClicked))
navItem.rightBarButtonItem = doneBtn

let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.width, 
    height: 44))
navBar.pushItem(navItem, animated: true)

nameTextField.inputAccessoryView = navBar
phoneNumberTextField.inputAccessoryView = navBar
 

(Xcode 8, Swift 3.0)

Wednesday, October 26, 2016

Dismiss Keyboard on Touch

If you want to dismiss the keyboard when you touch anywhere outside a text field, choose one of these ways:

Dismiss keyboard in a view with no scrollview/tableview

 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    view.endEditing(true)
}
 
(Note: This will not work on a scrollview or tableview.)


Dismiss keyboard when touching inside a scrollview/tableview

  1. Add a TapGestureRecognizer to your storyboard's scene
  2. Create an action outlet in your viewcontroller with the code below
 
@IBAction func TapGestureRecognizer_Action(_ sender: UITapGestureRecognizer) {
    view.endEditing(true)
}
 


All in code

 
override func viewDidLoad() {
    super.viewDidLoad()
    
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.endEditing))
    view.addGestureRecognizer(tapGesture)
}

func endEditing() {
    view.endEditing(true)
}
 
(Xcode 8, Swift 3.0)

Monday, June 6, 2016

Changing View When Showing Keyboard

 
var keyboardIsShowing = false

override func viewDidAppear(_ animated: Bool) {  
    NotificationCenter.default.addObserver(self, selector: 
        #selector(keyboardDidShow), 
        name: Notification.Name.UIKeyboardDidShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: 
        #selector(keyboardWillHide), 
        name: Notification.Name.UIKeyboardWillHide, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, 
        name: Notification.Name.UIKeyboardDidShow, object: self)
    NotificationCenter.default.removeObserver(self, 
        name: Notification.Name.UIKeyboardWillHide, object: self)
}
 

Not In A Scroll View

 
@objc 
func keyboardDidShow(notification: NSNotification) {
    
    if keyboardIsShowing { return }
    
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
        
    keyBoardIsShowing = true
        
    var newFrame = self.view.frame
    newFrame.origin.y -= keyboardSize.height
    self.view.frame = newFrame
}

@objc 
func keyboardWillHide(notification: NSNotification) {
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
        
    keyboardIsShowing = false
        
    var newFrame = self.view.frame
    newFrame.origin.y += keyboardSize.height
}
 
Note: While this works fine, I always had trouble getting it to show smoothly or even animate it. It is choppy. As of this date I recommend just putting your view in a scroll view and using the below methods for a smoother look and feel.

In A Scroll View

 
@IBOutlet weak var scrollView: UIScrollView!

@objc 
func keyboardDidShow(notification: NSNotification) {
    
    if keyboardIsShowing { return }
    
    let keyboardSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
        
    keyboardIsShowing = true
    
    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    // OPTIONAL: When you want bottom of a view to rest on the top of the keyboard.
    let viewBottom = myView.frame.origin.y + myView.frame.height
    let keyboardTop = view.bounds.height - keyboardSize.height

    if viewBottom > keyboardTop {
        let y = viewBottom - keyboardTop + 5
        scrollView.setContentOffset(CGPoint(x: 0, y: y), animated: true)
    }
}

@objc 
func keyboardWillHide(notification: NSNotification) {
    keyboardIsShowing = false

    let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets
}
 

(Xcode 9, Swift 4)

SwiftUI Search & Filter with Combine - Part 3 (iOS, Xcode 13, SwiftUI, 2...

In part 3 of the Searchable video series, I show you how to use Combine in #SwiftUI for the search and filter logic connected to the searcha...