0
0
iOS Swiftmobile~10 mins

Keyboard management in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to dismiss the keyboard when the user taps outside the text field.

iOS Swift
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  self.view.[1]()
}
Drag options to blanks, or click blank then click option'
AendEditing(true)
BbecomeFirstResponder()
CresignFirstResponder()
DreloadInputViews()
Attempts:
3 left
💡 Hint
Common Mistakes
Using becomeFirstResponder() which actually shows the keyboard.
Using resignFirstResponder() on the view, which is not correct.
2fill in blank
medium

Complete the code to move the view up when the keyboard appears.

iOS Swift
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)

@objc func keyboardWillShow(notification: NSNotification) {
  if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
    self.view.frame.origin.y = [1]
  }
}
Drag options to blanks, or click blank then click option'
A-keyboardSize.height
BkeyboardSize.height
C0
DkeyboardSize.width
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive keyboard height which moves the view down.
Using keyboard width instead of height.
3fill in blank
hard

Fix the error in the code to reset the view position when the keyboard hides.

iOS Swift
@objc func keyboardWillHide(notification: NSNotification) {
  self.view.frame.origin.y = [1]
}
Drag options to blanks, or click blank then click option'
Aself.view.frame.origin.y
Bself.view.frame.height
C-self.view.frame.height
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the view's height which moves the view off screen.
Using negative height which moves the view further up.
4fill in blank
hard

Fill both blanks to add observers for keyboard show and hide notifications.

iOS Swift
NotificationCenter.default.addObserver(self, selector: #selector([1]), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector([2]), name: UIResponder.keyboardWillHideNotification, object: nil)
Drag options to blanks, or click blank then click option'
AkeyboardWillShow
BkeyboardDidShow
CkeyboardWillHide
DkeyboardDidHide
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Did' notifications which happen after the keyboard animation.
Mixing show and hide selectors incorrectly.
5fill in blank
hard

Fill all three blanks to properly remove keyboard observers in deinit.

iOS Swift
deinit {
  NotificationCenter.default.[1](self, name: UIResponder.keyboardWillShowNotification, object: nil)
  NotificationCenter.default.[2](self, name: UIResponder.keyboardWillHideNotification, object: nil)
  NotificationCenter.default.[3](self)
}
Drag options to blanks, or click blank then click option'
AremoveObserver
BaddObserver
Cpost
DremoveAllObservers
Attempts:
3 left
💡 Hint
Common Mistakes
Using addObserver instead of removeObserver.
Trying to call a non-existent removeAllObservers method.