0

I have a strange problem with a ScrollView. I currently have 4 TextFields and 4 Labels on one ViewController, and when the keyboard pops up, it obstructs the view of 3 of the 4 pairs of TextFields and Labels. I added a ScrollView, but when I did, I was suddenly unable to click the TextFields and pop up the keyboard. Any ideas or alternative methods to fixing the obstruction of view?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

0

It sounds like you just threw a scrollview on top of all the labels and textfields. You need add all the textfields and labels as subviews of the scrollview, they should be dragged inside of it. You must setup the proper constraints and for the scrollview to have the correct content size based off of its content. You then need to update the constraints which effect the content size of the scrollview when the keyboard appears or dismissed, it does not all just auto work.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
  • Thank you for help! I added all proper objects as subviews and added the constraints, but it still won't scroll. Do you have any other suggestions? – Andrew Wilson May 01 '15 at 01:10
0

You probably have your new view on top of the text fields so when you clicking you are actually clicking in the view not in the text field. This is easy to see on the debug view from XCode. About the problem with the keyboard covering the test field, to fix this you will have to assigned to this two notifications

[[NSNotificationCenter defaultCenter] removeObserver:self
                                         name:UIKeyboardWillShowNotification
                                       object:nil];

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

and implement the code to move your fields out of the way when the keyboard pops and back to the normal position when it go away. Something like:

-(void)keyboardWillShow {
    // Animate the current view out of the way
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

-(void)keyboardWillHide {
    if (self.view.frame.origin.y >= 0)
    {
        [self setViewMovedUp:YES];
    }
    else if (self.view.frame.origin.y < 0)
    {
        [self setViewMovedUp:NO];
    }
}

There are many posts about it, a good example is "How to make a UITextField move up when keyboard is present" ate How to make a UITextField move up when keyboard is present?

I hope that help.

Community
  • 1
  • 1
Icaro
  • 14,585
  • 6
  • 60
  • 75
  • Thank you! I consulted the link you provided and I learned a lot, but I am still having some issues. I think I can figure it out later though – Andrew Wilson May 01 '15 at 01:11
  • With the new XCode it is easier to add all the content inside a scrollview and user the inbound properties to move the scrollview out of the way, you won't even need to bode with animation. – Icaro May 01 '15 at 01:27
0

First, you have to add all your UITextFields & UILabels inside the UIScrollView:

[self.scrollView addSubView:textField1];
[self.scrollView addSubView:label1];
[self.scrollView addSubView:textField2];
[self.scrollView addSubView:label1];
// ...

Then, don't forget to set your scrollView frame (usually in the initWithFrame or better yet in layoutSubviews). You also need to set the contentSizeproperty of your scrollView. That represents how big is the content inside that scrollView.

self.scrollView.frame = self.view.bounds;
self.scrollView.contentSize = self.view.bounds.size;

In your viewDidAppear, register for keyboard notifications:

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

Unregister for those notifications in the viewWillDisappear:

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

Finally, implement those keyboardWillShowNotification: and keyboardWillHideNotification: you gave as selector when you registered for notifications:

- (void)keyboardWillShowNotification:(NSNotification *)notification {
    CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    UIEdgeInsets insets = self.scrollView.contentInset;
    insets.bottom = keyboardFrame.size.height;
    self.scrollView.contentInset = insets;
}

- (void)keyboardWillHideNotification:(NSNotification *)notification {
    self.scrollView.contentInset = UIEdgeInsetsZero;
}

That way, when the keyboard is shown, you will be able to make the UIScrollView scroll to display all the content.

Rufel
  • 2,630
  • 17
  • 15