繁体   English   中英

如果键盘隐藏了ANY元素而不仅仅是文本字段,则将视图向上移动(不带导航栏)

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

我需要一种方法来检查显示的键盘是否在视图中隐藏了任何元素。 如果是这样,我需要以显示元素的方式向上移动视图,但不移动导航栏。 提前致谢

#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

声明一个名为keyboardHeight的CGFloat属性。

在您的viewDidLoad方法中:

// 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)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;

}

您还应该能够用self.view替换scrollView

当键盘出现时,您将减少可见区域的数量,因此,除非您具有可以以较小区域重新运行的布局,否则您真正想要的是滚动功能。

通常,您需要根据firstResponder字段选择滚动到的位置。 这将确保用户永远不会编辑他们看不到的字段。

修改您的视图层次结构,以将该UIScrollView包含在UIScrollView 另外,跟踪实例变量中哪个字段是firstResponder 然后,响应这样的键盘通知:

- (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];
    }
}

好吧,一个人可以使用像这样的库: https : //github.com/michaeltyson/TPKeyboardAvoiding ,或者他可以通过编程来实现。 这是我的工作方式:

//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];
    }

然后添加这些选择器方法

#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;
    }];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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