0

I have a view that looks like this:

When the user tries to enter information the keyboard covers the UITextViews I thought trying to detect a tap on the UITextView using a UIGestureRecognizer that I added to the UITextView, and then change the constraints according to the view which was tapped. When I tap it the app crashes.

This is the code I used to detect a tap on the UITextViews -

//in viewDidLoad
let tap = UITapGestureRecognizer(target: self.textYoutube, action: "handleTap")
self.textYoutube.addGestureRecognizer(tap)    

func handleTap(tap: UITapGestureRecognizer) {
    if (tap.state == UIGestureRecognizerState.Ended) {
        println("[handleTap] Tap ended")
    }
}    

Is there something wrong with my code? Is there a better way of moving the UITextViews?

Community
  • 1
  • 1
Omer Nave
  • 45
  • 1
  • 8
  • Why don't you use https://github.com/michaeltyson/TPKeyboardAvoiding – Shoaib Mar 09 '15 at 19:20
  • This is an X/Y question because Apple details using a scroll view and adjusting the insets for this common situation in "[Managing the Keybaord](https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html)". – Marcus Adams Mar 09 '15 at 20:09
  • possible duplicate of [How to make a UITextField move up when keyboard is present](http://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) – Marcus Adams Mar 09 '15 at 20:10
  • Shoaib, Is there something similar in Swift? – Omer Nave Mar 09 '15 at 20:21

2 Answers2

1

To detect the tap on the textfield; implement the textField's delegate method textFieldShouldBeginEditing:

Shoaib
  • 2,286
  • 1
  • 19
  • 27
0

Use the notifications, they supply the keyboard frame, with the new keyboards the size may vary and the frame will allow moving the input field above it.

Register for these notifications and handle moving the view out of the way of the keyboard:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];

Get the keyboard frame from the userInfo:

CGRect keyboarddRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];

Release the notification in dealloc"

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

Alternately use the delegate methods:

textViewDidBeginEditing:
textViewDidEndEditing:
zaph
  • 111,848
  • 21
  • 189
  • 228