iOS中设置点击textField 在textField上面的控
2016-04-17 本文已影响443人
方圆十里不留母狗
首先
//注册通知:键盘frame位置和尺寸发生改变的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
然后
// 获得屏幕的高度
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
// 获得通知信息
NSDictionary *userInfo = note.userInfo;
// 获得键盘显示完毕或隐藏完毕的frame
CGRect keyboardFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 获得键盘的y值
CGFloat keyboardY = keyboardFrame.origin.y;
// 计算平移的距离
CGFloat translationY = keyboardY - screenH ;
// 平移控制器的view
// 显示平移的距离是:-258 隐藏是 0
self.view.transform = CGAffineTransformMakeTranslation(0, translationY);