zoukankan      html  css  js  c++  java
  • 动态获取键盘高度

    //在遇到有输入的情况下。由于现在键盘的高度是动态变化的。中文输入与英文输入时高度不同。所以输入框的位置也要做出相应的变化
    #pragma mark - keyboardHight
    -(void)viewWillAppear:(BOOL)animated
    {
        [self registerForKeyboardNotifications];
    }
    
    -(void)viewWillDisappear:(BOOL)animated
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
    - (void)registerForKeyboardNotifications
    {
        //使用NSNotificationCenter 鍵盤出現時
        [[NSNotificationCenter defaultCenter] addObserver:self
         
                                                 selector:@selector(keyboardWasShown:)
         
                                                     name:UIKeyboardDidShowNotification object:nil];
        
        //使用NSNotificationCenter 鍵盤隐藏時
        [[NSNotificationCenter defaultCenter] addObserver:self
         
                                                 selector:@selector(keyboardWillBeHidden:)
         
                                                     name:UIKeyboardWillHideNotification object:nil];
         
        
    }
    
    
    
    //实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
    - (void)keyboardWasShown:(NSNotification*)aNotification
    {
        NSDictionary* info = [aNotification userInfo];
        //kbSize即為鍵盤尺寸 (有width, height)
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
        NSLog(@"hight_hitht:%f",kbSize.height);
        if(kbSize.height == 216)
        {
            keyboardhight = 0;
        }
        else 
        {
            keyboardhight = 36;   //252 - 216 系统键盘的两个不同高度
        }
        //输入框位置动画加载
        [self begainMoveUpAnimation:keyboardhight];
    }
    
    //当键盘隐藏的时候
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification
    {
         //do something
    }
    
    
    //(TextView) 当键盘开始输入前。时行计算与动画加载
    -(void)textViewDidBeginEditing:(UITextView *)textView
    {
        NSLog(@"gegin animation");
        sendMsgTextView =textView;
        resultCommunityTableview.frame = CGRectMake(0, 36, 320, 150);
        //动画加载
        [self begainMoveUpAnimation:0.0 ];
        
    }
    
    
    //关闭键盘(TextView) 换行时。隐藏键盘
    -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text  
    {  
        resultCommunityTableview.frame = CGRectMake(0, 36, 320, 376);
        if ([text isEqualToString:@"
    "]) {  
            [textView resignFirstResponder];  
            return NO;  
        }
        return YES;  
    }  
    
    
    
    //输入结束时调用动画(把按键。背景。输入框都移下去)
    -(void)textViewDidEndEditing:(UITextView *)textView
    {
        NSLog(@"tabtabtab");
        [self endEditAnimation];
        
        //释放
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    }
    
    
    
    //判断当前输入法
    -(void)textViewDidChangeSelection:(UITextView *)textView
    {
        NSLog(@"wewe:%@",[[UITextInputMode currentInputMode] primaryLanguage]);
        /*
        if ([[UITextInputMode currentInputMode] primaryLanguage] == @"en-US") { 
            NSLog(@"en-US"); 
        } 
        else 
        { 
            NSLog(@"zh-hans"); 
        } 
         */
    }
  • 相关阅读:
    try? try! try do catch try 使用详解
    Swift Write to file 到电脑桌面
    NSLayoutConstraint 使用详解 VFL使用介绍
    automaticallyAdjustsScrollViewInsets 详解
    Swift 给UITableView 写extension 时 报错 does not conform to protocol 'UITableViewDataSource'
    OC Swift中检查代码行数
    Swift中 @objc 使用介绍
    SWift中 '?' must be followed by a call, member lookup, or subscript 错误解决方案
    Swift 中 insetBy(dx: CGFloat, dy: CGFloat) -> CGRect 用法详解
    求1000之内所有“完数”(注:C程序设计(第四版) 谭浩强/著 P141-9)
  • 原文地址:https://www.cnblogs.com/hereiam/p/3932477.html
Copyright © 2011-2022 走看看