zoukankan      html  css  js  c++  java
  • IOS键盘弹出文本输入框上移

    刚开始做IOS,做的不好,还望朋友们多多指教,谢谢!

    #import "ViewController.h"
    
    @interface ViewController ()<UITextFieldDelegate>
    {
        UIView *activeView;
        UITextField *textField;
        float keyBoardHeight;
        float w, h;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        w = self.view.bounds.size.width;
        h = self.view.bounds.size.height;
        
        self.view.backgroundColor = [UIColor whiteColor];
        
        activeView = [[UIView alloc] initWithFrame:CGRectMake(0, h - 40, w, 45)];
        activeView.backgroundColor = [UIColor colorWithRed:0.4 green:0.3 blue:0.5 alpha:1.0];
        [self.view addSubview:activeView];
        
        
        textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, w-20, 25)];
        textField.backgroundColor = [UIColor lightGrayColor];
        textField.keyboardAppearance = UITextBorderStyleRoundedRect;
        textField.delegate = self;
        [activeView addSubview:textField];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    }
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
        [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillShowNotification object:nil];
    
        return YES;
    }
    
    - (void)keyboardWillAppear:(NSNotification*)noti{
        
        NSDictionary *keyBInfo = [noti userInfo];
        NSValue *value = [keyBInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardFrame = [value CGRectValue];
        keyBoardHeight = keyboardFrame.size.height;
        
        [UIView animateWithDuration:1 animations:^{
            CGRect tFrame = activeView.frame;
            tFrame.origin.y = h - keyBoardHeight - 40;
            activeView.frame = tFrame;
        }];
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        [textField resignFirstResponder];
        [UIView animateWithDuration:0.3 animations:^{
            CGRect tFrame = activeView.frame;
            tFrame.origin.y = h - 40;
            activeView.frame = tFrame;
        }];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end
  • 相关阅读:
    利用带关联子查询Update语句更新数据
    Marriage for Love
    Process Multiple Items respectively by commas!
    How to refresh current Form when thorugh X++ code influence
    Java SPI 机制分析
    浅谈微服务落地实践
    分布式事务之最大努力通知
    分布式事务之三阶段提交
    分布式事务之事务概念剖析
    SQL之树形查询结构设计
  • 原文地址:https://www.cnblogs.com/garywong1949/p/5410725.html
Copyright © 2011-2022 走看看