0

In my swift 3 project, I'm using UITextFields and UITextViews in my screens and when keyboard appears some of them are overlapped by the keyboard and with the different devices the overlapping views are get differ. For instance, the text fields overlapped in iPhone SE may not overlapped in iPhone 7 plus.

So to scroll up the views how can I determine the views that are gonna overlapped by the keyboard. is there any way of doing this.

I tried to determine the position of the textView in order to determine weather it's overlapped by keyboard or not. but it gives me the same values for smaller and bigger devices. following is what I did.

print(" textfiled position : \(notesTextView.frame)")
print(" X position : \(notesTextView.frame.origin.x)")
print(" Y position : \(notesTextView.frame.origin.y)")
print(" width : \(notesTextView.frame.size.width)")
print(" Height : \(notesTextView.frame.size.height)")
print("Keyboard Height : \(keyboardHeightValue)")

and these are the values i'm getting.

no matter where the UITextView is in the screen and no matter which device am checking once I touch the UITextView it returns the same 108.0 Y value

 textfiled position : (23.0, 108.0, 274.0, 150.0)
 X position : 23.0
 Y position : 108.0
 width : 274.0
 Height : 150.0
 Keyboard Height : 297.0
 max Height : 568.0
 Screen Height : 320.0
 Screen Height : 568.0

I think the way I'm getting the coordinates of the text view may wrong as in my case I need to get the coordinates of the screen.In this case I've taken the position of the view.But as I'm using a table view controller I cannot use below code snippet.

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let position = touch.location(in: self.notesTextView)
            location = touch.location(in: self.notesTextView)
            print(position.x)
            print(position.y)
        }
    }

how can I get the absolute coordinations of the view.

Thanks and regards!

Samantha Withanage
  • 3,811
  • 10
  • 30
  • 59
  • user: https://github.com/michaeltyson/TPKeyboardAvoiding – Nitin Gohel May 18 '17 at 06:02
  • 1
    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) – Tj3n May 18 '17 at 06:04
  • I think this question has been asked many times, try those question on the right to help – Tj3n May 18 '17 at 06:05
  • check : http://stackoverflow.com/questions/40839366/how-to-solve-keyboard-problems-in-swift-3/42361236#42361236 – Priyal May 18 '17 at 06:20
  • @Tj3n this is not about scrolling. this is about determining what views to scroll – Samantha Withanage May 18 '17 at 06:33
  • It doesnt matter, determining can be done by many ways, by getting the height of the keyboard n compare it with the view's frame/constraint/scrollview content offset, then you can tell if it need to scrollup – Tj3n May 18 '17 at 07:37
  • @ Tj3n : I tried to get the position of the textView i'm using.But it gives me same x and y values for smaller devices and bigger devices. will add my work around to the question. – Samantha Withanage May 18 '17 at 08:10

1 Answers1

0

Using Objective C you can use this code, I have no experience with Swift so you can change syntax, its working fine for me in objective c. keyBoardAppeared: is a notification gets fire when keyboard appear.

- (void)textFieldDidBeginEditing:(UITextField *)textField{

  activeField = textField; // Where activeField is UIView
}


-(void)keyBoardAppeared:(NSNotification *)note{

    NSDictionary *info = [note userInfo];
    keyBoardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] 
    CGRectValue].size;

    CGRect subViewPosition = [self.view convertRect:activeField.frame 
    fromView:[activeField superview]];

    CGRect aRect = self.view.frame;
    aRect.size.height -= keyBoardSize.height;

   if (!CGRectContainsPoint(aRect, subViewPosition.origin) ) {

    CGPoint newOffset = CGPointMake(0, subViewPosition.origin.y - 
    keyBoardSize.height);

    [mainScrollView setContentOffset:newOffset];
}}
Nisar Ahmad
  • 885
  • 1
  • 10
  • 25