zoukankan      html  css  js  c++  java
  • 为 UITextField 增加键盘偏移的模板化写法

    .h代码

    #import
    <UIKit/UIKit.h> @interface ViewController : UIViewController<UITextFieldDelegate> { int i_offset; //偏移量 int i_textFieldY; //textField 的y 值 int i_textFieldHeight; //textField的高度 IBOutlet UITextField *uitf_textField1; IBOutlet UITextField *uitf_textField2; IBOutlet UITextField *uitf_textField3; } @end

    .m代码

    #import "ViewController.h"
    
    #define StautsBarHeight 20.0f
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        uitf_textField1.delegate = self;
        uitf_textField2.delegate = self;
        uitf_textField3.delegate = self;
        
        i_offset = 0;    //默认偏移量为0
        i_textFieldY = 0;  
        i_textFieldHeight = 0;
        
        //注册键盘监听消息
        [self registerKeyBoardNotification];
        
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    #pragma mark- UITextFieldDelegateMethod
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField
    {        
        NSLog(@"进入textFieldDidBeginEditing");  
        
        i_textFieldY = textField.frame.origin.y;
        i_textFieldHeight = textField.frame.size.height;
                    
    }
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField
    {            
        [textField resignFirstResponder];
        return YES;        
    }
    
    #pragma mark- 键盘通知事件 [核心代码]
    
    
    //注册键盘监听消息
    -(void)registerKeyBoardNotification
    {
        //增加监听,当键盘出现或改变时收出消息    [核心代码]
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        
        //增加监听,当键退出时收出消息
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
        // 键盘高度变化通知,ios5.0新增的 
    #ifdef __IPHONE_5_0
        float version = [[[UIDevice currentDevice] systemVersion] floatValue];
        if (version >= 5.0) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];
        }
    #endif
    }
    
    
    //当键盘出现或改变时调用
    - (void)keyboardWillShow:(NSNotification *)aNotification
    {
        //获取键盘的高度
        NSDictionary *userInfo = [aNotification userInfo];
        NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardRect = [aValue CGRectValue];
        int keyboardHeight = keyboardRect.size.height;
        
        //计算偏移量
        i_offset = keyboardHeight - (self.view.frame.size.height-(i_textFieldY+i_textFieldHeight));
        
        //进行偏移
        NSTimeInterval animationDuration = 0.30f;                
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];                
        [UIView setAnimationDuration:animationDuration];
        float width = self.view.frame.size.width;                
        float height = self.view.frame.size.height;  
        if(i_offset > 0)                                         
        {
            CGRect rect = CGRectMake(0.0f,StautsBarHeight-i_offset,width,height); //把整个view 往上提,肯定要用负数 y                
            self.view.frame = rect;        
        }  
        
        [UIView commitAnimations]; 
    }
    
    //当键退出时调用
    - (void)keyboardWillHide:(NSNotification *)aNotification
    {
        if(i_offset > 0)                                         
        {
            //恢复到偏移前的正常量
            NSTimeInterval animationDuration = 0.30f;                
            [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];                
            [UIView setAnimationDuration:animationDuration];
            float width = self.view.frame.size.width;                
            float height = self.view.frame.size.height;  
            CGRect rect = CGRectMake(0.0f,StautsBarHeight,width,height); //把整个view 往上提,肯定要用负数 y   注意self.view 的y 是从20开始的,即StautsBarHeight 
            self.view.frame = rect;  
            
            [UIView commitAnimations]; 
        }  
        
        i_offset = 0;
    }
  • 相关阅读:
    git 命令行下浏览器tig使用记录
    根据进程名字杀死进程
    centos7 在线安装postgresql9.5
    HttpClient使用代理访问
    nmap查看服务器端口使用情况
    java后台启动jar包
    不同语言,系统通过共享内存方式实现信息交互
    释放 MappedByteBuffer映射的内存
    Java使用ByteBuffer读取大文件
    Java共享内存
  • 原文地址:https://www.cnblogs.com/ygm900/p/3152287.html
Copyright © 2011-2022 走看看