动态获取键盘高度

//在遇到有输入的情况下。由于现在键盘的高度是动态变化的。中文输入与英文输入时高度不同。所以输入框的位置也要做出相应的变化
#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:@"\n"]) {
        [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");
    }
     */
}
时间: 2024-08-09 23:53:59

动态获取键盘高度的相关文章

ios动态获得键盘高度,并改变对话框的位置

NSNotificationCenter:键盘出现.消失时的通知 UIKeyboardWillShowNotification;UIKeyboardDidShowNotification;UIKeyboardWillHideNotification;UIKeyboardDidHideNotification; 在要使用键盘的视图控制器中(既viewDidLoad中),接收键盘事件的通知: - (void) registerForKeyboardNotifications{ //键盘改变时候会调用

iOS键盘监听以及获取键盘高度

在文本输入时,界面会弹出键盘.有时,当文本输入框过低,被键盘遮挡,使用户无法看见输入框文本内容,这就使得用户体验过低. 所以需要我们对键盘进行监控并获取键盘高度,调节界面或文本框高度进行处理.如下图,文本输入框过低的情况: 像这样的情况,如若未作处理,输入框就会被键盘遮挡.这时,我们需要监听键盘事件,获取键盘高度,对文本框视图进行高度调整: 1 #import "ViewController.h" 2 3 @interface ViewController ()<UITextFi

从动态获取div高度的问题展开来看

ps 可能篇幅比较长,请大家耐心看看 今天有人在群里问我 动态获取高度怎么获取  我就说jq中的outerHeight. height .innerHeight   原生的height clientHeight.scrollHeight.offsetHeight  按道理百度一下 就ok 了  但是他问了一句这些有什么区别? 哎呦 我去 我还真的需要整理一下   好吧 咱们开始整理一下 <!DOCTYPE html> <html lang="en"> <h

iOS 获取键盘高度

- (void)viewDidLoad { [super viewDidLoad]; //增加监听,当键盘出现或改变时收出消息 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //增加监听,当键退出时收出消息 [[NSNotificationCenter defa

swift获取键盘高度

列表内容 监听键盘起落 下面是可以监听到得四种状态 UIKeyboardWillShowNotification: String UIKeyboardDidShowNotification: String UIKeyboardWillHideNotification: String UIKeyboardDidHideNotification: String NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyb

android监听软键盘事件并获取键盘高度

@Override public void onResume(){ super.onResume(); //获取当前屏幕内容的高度 getWindow().getDecorView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() { @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldL

uni-app 动态获取元素高度等

uni.getSystemInfo({ success: function(res) { // res - 各种参数    console.log(res.windowWidth); // 屏幕的宽度     let info = uni.createSelectorQuery().select(".类名");   info.boundingClientRect(function(data) { //data - 各种参数   console.log(data.width)  // 获

react native获取键盘高度

componentWillUnmount() { this.keyboardDidShowListener.remove(); this.keyboardDidHideListener.remove();} componentWillMount() { this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow.bind(this)); this.keyboardDidH

Js动态获取iframe子页面的高度总结

问题的缘由 产品有个评论列表引用的是个iframe,高度不固定于是引发这个总结. 方法1:父级页面获取子级页面的高度 给元素设置高度 这方法是用在父级页面里的,通过获取子级页面的高度给iframe设置高度 涉及了一些兼容问题: IE用attachEvent | 3C用onload来判断子页面是否加载完成. IE用contentWindow | 3C用contentDocument来获取子页面 IE用document.documentElement.scrollHeight(兼容ie6 ie7)|