//
// ViewController.m
// 控制键盘显示页面自动上升
//
// Created by dc0061 on 16/1/4.
// Copyright © 2016年 dc0061. All rights reserved.
//
#import "ViewController.h"
ViewController ()
{
UITextField *text;
UITextField *text1;
UILabel *label;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 175, 40)];
label.text=@"测试";
[self.view addSubview:label];
text = [[UITextField alloc]initWithFrame:CGRectMake(100, 550, 175, 40)];
text.textAlignment=NSTextAlignmentCenter;
text.layer.cornerRadius=5;//设置显示圆角
text.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.9];
[self.view addSubview:text];
text1 = [[UITextField alloc]initWithFrame:CGRectMake(100, 600, 175, 40)];
text1.textAlignment=NSTextAlignmentCenter;
text1.layer.cornerRadius=5;//设置显示圆角
text1.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.9];
[self.view addSubview:text1];
//在输入框左侧添加一个view
UIView *views=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 1)];
text.leftView=views;
text.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:views];
//监听键盘事件
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(keyboard:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
#pragma mark 代理方法
- (void)keyboard : (NSNotification *) noteInfo{
//获取键盘的高度
CGRect rect=[noteInfo.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSLog(@"%@",noteInfo.userInfo);
self.view.transform=CGAffineTransformMakeTranslation(0, rect.origin.y-self.view.frame.size.height);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark view响应事件
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//隐藏键盘
[text resignFirstResponder];
[text1 resignFirstResponder];
}
@end