How to re-size UITextView when keyboard shown with iOS 7

后端 未结 8 1412
耶瑟儿~
耶瑟儿~ 2020-12-05 03:34

I have a view controller which contains a full-screen UITextView. When the keyboard is shown I would like to resize the text view so that it is not hidden under

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 04:08

    i had done it and its work completely.

      #define k_KEYBOARD_OFFSET 95.0
    
    -(void)keyboardWillAppear {
        // Move current view up / down with Animation
        if (self.view.frame.origin.y >= 0)
        {
            [self moveViewUp:NO];
        }
        else if (self.view.frame.origin.y < 0)
        {
            [self moveViewUp:YES];
        }
    }
    
    -(void)keyboardWillDisappear {
        if (self.view.frame.origin.y >= 0)
        {
            [self moveViewUp:YES];
        }
        else if (self.view.frame.origin.y < 0)
        {
            [self moveViewUp:NO];
        }
    }
    
    -(void)textFieldDidBeginEditing:(UITextField *)sender
    {
        //if ([sender isEqual:_txtPassword])
       // {
            //move the main view up, so the keyboard will not hide it.
            if  (self.view.frame.origin.y >= 0)
            {
                [self moveViewUp:YES];
            }
        //}
    }
    
    //Custom method to move the view up/down whenever the keyboard is appeared / disappeared
    -(void)moveViewUp:(BOOL)bMovedUp
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.4]; // to slide the view up
    
        CGRect rect = self.view.frame;
        if (bMovedUp) {
            // 1. move the origin of view up so that the text field will come above the keyboard
            rect.origin.y -= k_KEYBOARD_OFFSET;
    
            // 2. increase the height of the view to cover up the area behind the keyboard
            rect.size.height += k_KEYBOARD_OFFSET;
        } else {
            // revert to normal state of the view.
            rect.origin.y += k_KEYBOARD_OFFSET;
            rect.size.height -= k_KEYBOARD_OFFSET;
        }
    
        self.view.frame = rect;
    
        [UIView commitAnimations];
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        // register keyboard notifications to appear / disappear the keyboard
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillAppear)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
    
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillDisappear)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        // unregister for keyboard notifications while moving to the other screen.
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIKeyboardWillShowNotification
                                                      object:nil];
    
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:UIKeyboardWillHideNotification
                                                      object:nil];
    }
    

提交回复
热议问题