zoukankan      html  css  js  c++  java
  • 完美解决ios4与ios5输入框随键盘移动问题

    iOS5中当键盘输入法切换到中文时,键盘高度由216增加到252像素。这一变化将遮住输入框。如何才能解决这一问题呢?

          在iOS5中,新增有notification(UIKeyboardWillChangeFrameNotification)可以用来监测键盘frame的变化。在iOS4中,可以通过UIKeyboardWillShowNotification以及UIKeyboardWillHideNotification来监测键盘的显示与隐藏事件。综合处理下,可以用以下方法解决:

    #ifndef IOS_VERSION  
    #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]  
    #endif  

    在viewdidload中注册监测事件:

    if (IOS_VERSION < 5.0) {  
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillShowNotification object:nil];  
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillHideNotification object:nil];  
    }else{  
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];  
    }  

    在dealloc中移除监测事件:

    if (IOS_VERSION < 5.0) {  
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];  
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];  
    }else{  
        [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillChangeFrameNotification object:nil];  
    } 

    事件处理函数:

    - (void)keyboardWillChangeFrame:(NSNotification *)notification{  
        if(!isDisplayFaceBox){  
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2  
            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {  
    #endif  
    #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_2  
                NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];  
    #else  
                NSValue *keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardBoundsUserInfoKey];  
    #endif  
                CGRect keyboardBounds;  
                [keyboardBoundsValue getValue:&keyboardBounds];  
                //UIEdgeInsets e = UIEdgeInsetsMake(0, 0, keyboardBounds.size.height, 0);  
                //[keyboardScrollView setScrollIndicatorInsets:e];  
                //[keyboardScrollView setContentInset:e];  
                  
                NSInteger offset = 480-keyboardBounds.origin.y;  
                CGRect listFrame = CGRectMake(0, offset, 320, 377-offset);  
                  
                [UIView beginAnimations:@"anim" context:NULL];  
                [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];  
                [UIView setAnimationBeginsFromCurrentState:YES];  
                [UIView setAnimationDuration:0.3];  
                //处理移动事件,将各视图设置最终要达到的状态  
                [tableviewbo setFrame:listFrame];  
                  
                faceButton.hidden = NO;  
                keyboardButton.hidden = YES;      
                  
                [keyboardScrollView setContentOffset:CGPointMake(0, offset) animated:NO];  
                  
                [self scrollToBottomAnimated:NO];  
                  
                [UIView commitAnimations];  
                  
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_3_2  
            }  
    #endif  
        }  
    }

    原文地址:http://blog.csdn.net/zengconggen/article/details/7071086

  • 相关阅读:
    HDU 1717 小数化分数2(最大公约数)
    C#计数器
    C#计数器
    c#计算器
    c#计算器
    PHP 错误与异常 笔记与总结(18 )页面重定向实现
    想使用 MongoDB ,你应该了解这8个方面!
    大数据代表未来,投资力度增强
    统计学和数据挖掘的异同探讨
    统计学和数据挖掘的异同探讨
  • 原文地址:https://www.cnblogs.com/appwgh/p/2517417.html
Copyright © 2011-2022 走看看