简体   繁体   中英

Enable scrolling content when keyboard appears in iOS

I have an application on iPhone who has much content in viewcontroller. Generally the main content is form. My problem is that I put up keyboard and want to type some value to form fields much of content is invisible. I want to scroll this content when keyboard appear. How can I do this? My view does'n have scroll view however I tried do this but it don't works.

All hints I found so far concern how to put up used textbox, but this is not my assumption.

This is what i did, I changed the top constraint when keyboard rises up and down like this-

in viewDidLoad call this method-

   [self observeKeyboard];
#pragma mark- Keyboard Listen Methods
- (void)observeKeyboard {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSValue *kbFrame = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
CGRect keyboardFrame = [kbFrame CGRectValue];
CGFloat height = keyboardFrame.size.height;
self.COnstraintTopViewVertical.constant=(value of constant)-height;
[UIView animateWithDuration:animationDuration animations:^{
    [self.view layoutIfNeeded];
}];
}

- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary *info = [notification userInfo];
NSTimeInterval animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
self.ConstraintTopViewVertical.constant=reset value;
[UIView animateWithDuration:animationDuration animations:^{
    [self.view layoutIfNeeded];
}];
}

You can programmatically change position in the view upon keyboard appearance with the following code:

func keyboardWasShown(notification: NSNotification) {

    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.setContentOffset(CGPoint(x: setXOffsetHere, y: setYOffsetHere), animated: true)
    })
}

This code will transition the view to the coordinates you specify in .setContentOffset call after keyboard appearance. You still have to get the coordinates, but usually they are pretty straightforward to get from storyboard.

  1. Put all your views in a scroll view
  2. Set an auto layout constraint from the bottom of the scroll view to the bottom of the superview
  3. In viewWillAppear

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; 
  4. Handle the KB notification

     - (void)keyboardWillChangeFrame:(NSNotification*)aNotification { CGRect kbFrame = [aNotification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGRect kbWindowIntersectionFrame = CGRectIntersection(self.view.window.bounds, kbFrame); [UIView animateWithDuration:[aNotification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{ self.scrollVBottomConstraint.constant = kbWindowIntersectionFrame.size.height; [self.view layoutIfNeeded]; }]; } 
what i got from your question is: you are using textfield on UIView which is not on the scrollview, now you have to scroll the UIView when keyboard appears. just do following stuff.
-Give the tag to your textfield.
-suppose tag you have given is 4545646
-do not forgot to assign delegate to your textfield (it will execute below delegate method when you start to edit)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField.tag == 4545646) {
        UIView *contanerView = [self.view viewWithTag:5556466];
        [UIView animateWithDuration:0.5 animations:^{
            contanerView.frame = CGRectMake(self.view.frame.origin.x+1, -85, self.view.frame.size.width, self.view.frame.size.height-9);
        }];
    }
}
Change the frame as per your requirement.
and 
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.tag == 4545646) {
        [textField resignFirstResponder];
        UIView *contanerView = [self.view viewWithTag:5556466];
        [UIView animateWithDuration:0.5 animations:^{
            contanerView.frame = CGRectMake(self.view.frame.origin.x+1, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
        }];
}

In viewDidAppear add the following notifications

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

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

and in keyBoardShown method get the keyboard height

 - (void)keyBoardShown:(NSNotification*)notification
{
    if(self.view.frame.origin.y>=0)
    {
        NSDictionary* keyboardInfo = [notification userInfo];
        NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

        //    NSLog(@"hihi : %f",keyboardFrameBeginRect.size.height);

        keyBoardHeight = keyboardFrameBeginRect.size.height-yDeault+50;


        if (keyBoardHeight>0) {
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:0.5];

            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y-keyBoardHeight), self.view.frame.size.width, self.view.frame.size.height);
            [UIView commitAnimations];
        }


    }
}

in keyBoardDismiss setback the mainview y coordinate to previous value

-(void)keyBoardDismiss:(NSNotification*)notification{
    if(self.view.frame.origin.y<0){
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];

        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        self.view.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.origin.y+keyBoardHeight), self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

here yDeault is textfield y coordinate value

 yDeault= screenHeight-textField.frame.origin.y+textField.frame.size.height;

Once the keyboard is shown main view will be moved up and once keyboard is dismissed main view will be moved back to the original position

OK. I done this. I need to just add scroll view but in my app probably I have to reconstruct all my view.

No need to add scrollview. Directly you can do this. You can change the self.view.frame without the need of scrollview.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM