简体   繁体   中英

Moving the view up (without the navigation bar) if keyboard hides ANY element and not only a text field

I need a way to be able to check if the keyboard when it shows up hides any element in the view. If so, i need the view to move up in a way that the element is shown but without the navigation bar moving. Thanks in advance

#import "RequestViewController.h"
#define kOFFSET_FOR_KEYBOARD 80.0

@interface RequestViewController ()

@end

@implementation RequestViewController{
    CGFloat keyboardHeight;
}
@synthesize descirptionTextView;
@synthesize scrollView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view
    descirptionTextView.text = @"Comment";
    descirptionTextView.textColor = [UIColor lightGrayColor];
    descirptionTextView.delegate = self;

    descirptionTextView.layer.cornerRadius = 8;
    // border
    [descirptionTextView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [descirptionTextView.layer setBorderWidth:0.5f];

    // drop shadow
    [descirptionTextView.layer setShadowColor:[UIColor blackColor].CGColor];
    [descirptionTextView.layer setShadowOpacity:0.8];
    [descirptionTextView.layer setShadowRadius:3.0];
    [descirptionTextView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];

    // register for keyboard notifications
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

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


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
    descirptionTextView.text = @"";
    descirptionTextView.textColor = [UIColor blackColor];
    return YES;
}

-(void) textViewDidChange:(UITextView *)textView
{

    if(descirptionTextView.text.length == 0){
        descirptionTextView.textColor = [UIColor lightGrayColor];
        descirptionTextView.text = @"Comment";
        [descirptionTextView resignFirstResponder];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Scrolling out of keyboard way

-(void)keyboardWillShow:(NSNotification *)nsNotification{

    //first, get height of keyboard
    NSDictionary *userInfo = [nsNotification userInfo];
    CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    keyboardHeight = kbRect.size.height;
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, self.view.frame.size.height - keyboardHeight - scrollView.frame.origin.y);
    return;

}

-(void)keyboardWillHide{

    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + keyboardHeight - 40 - 40 - 14 + scrollView.frame.origin.y);
    return;

}

您可以在键盘委托方法中重绘视图:keyboardWillShow keyboardWillHide

Declare a CGFloat property named keyboardHeight.

In your viewDidLoad method:

// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];

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

Keyboard methods:

-(void)keyboardWillShow:(NSNotification *)nsNotification{

    //first, get height of keyboard
    NSDictionary *userInfo = [nsNotification userInfo];
    CGRect kbRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];

    keyboardHeight = kbRect.size.height;
    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, self.view.frame.size.height - keyboardHeight - scrollView.frame.origin.y);
    return;

}

-(void)keyboardWillHide{

    scrollView.frame = CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height + keyboardHeight - 40 - 40 - 14 + scrollView.frame.origin.y);
    return;

}

You should also be able to substitute scrollView with self.view

When the keyboard appears, you are reducing the visible area by a considerable amount, so unless you have a layout that can be re-run with the smaller area, what you really desire is the ability to scroll.

In general, you'll want to choose where you scroll to based on which field is the firstResponder. This will guarantee that a user is never editing a field that they cannot see.

Revise your view hierarchy for this controller to be contained within a UIScrollView . Also, track which field is the firstResponder in an instance variable. Then, respond to keyboard notifications like this:

- (void)keyboardWillShow:(NSNotification*)notification
{
    NSValue* keyboardFrameValue = [notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect kbRect = [self.view convertRect:keyboardFrameValue.CGRectValue fromView:nil];
    CGRect overlap = CGRectIntersection(self.view.bounds, kbRect);

    self.scroller.contentInset = UIEdgeInsetsMake(self.scroller.contentInset.top, 0, overlap.size.height, 0);

    if (self.firstResponderView)
    {
        CGRect fieldRect = [self.scroller convertRect:self.firstResponderView.frame fromView:self.firstResponderView.superview];
        [self.scroller scrollRectToVisible:fieldRect animated:YES];
    }
}

Well, one can use a library like this one: https://github.com/michaeltyson/TPKeyboardAvoiding , or he can do it programmatically. This is how I proceeded:

//adding the notification about when keyboard appears and disappears when the view loads and remove them when the view will disappear    
- (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }

    - (void)viewWillDisappear:(BOOL)animated {
        [super viewWillDisappear:animated];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }

Then you add these selector methods

#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    if(keyboardSize.height> (self.view.frame.size.height - YourTextField.frame.size.height -YourTextField.frame.origin.y)){
        [UIView animateWithDuration:0.3 animations:^{
            CGRect f = self.view.frame;
            f.origin.y = self.view.frame.size.height - YourTextField.frame.size.height -YourTextField.frame.origin.y-keyboardSize.height - 10;
            self.view.frame = f;
        }];

    }

}

-(void)keyboardWillHide:(NSNotification *)notification
{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect f = self.view.frame;
        f.origin.y = 0.0f;
        self.view.frame = f;
    }];
}

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