UIScrollView中UITextField文本编辑框位置随键盘的显示(隐藏)改变
来源:互联网 发布:淘宝差评回复技巧 编辑:程序博客网 时间:2024/06/06 09:19
//
// ViewBaseController.h
//
//
// Created bynokiaxjw on 12-12-6.
// Copyright (c) 2012年 All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewBaseController :UIViewController<UITextFieldDelegate>
@property(nonatomic,strong)UITextField *activeTextField;
@property(nonatomic,assign)NSUInteger keyboardHeight;
//UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField;
- (void)textFieldDidEndEditing:(UITextField *)textField;
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
- (IBAction)onEditDone:(id)sender;
@end
//
// ViewBaseController.m
// Sb06D
//
// Created bynokiaxjw on 12-12-6.
// Copyright (c) 2012年. All rights reserved.
//
#import "ViewBaseController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
@interfaceViewBaseController ()
@end
@implementation ViewBaseController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [superinitWithCoder:aDecoder];
if (self) {
self.keyboardHeight = 352.0f;
self.activeTextField =nil;
}
returnself;
}
- (void)dealloc
{
[[NSNotificationCenterdefaultCenter]removeObserver:self];
}
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view.
AppDelegate *delegate = (AppDelegate*)[[UIApplicationsharedApplication]delegate];
self.device = delegate.device;
self.pile =self.device.pile;
[selfregisterForKeyboardNotifications];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) registerForKeyboardNotifications
{ [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillShow:)name:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(keyboardWillHide:)name:UIKeyboardWillHideNotificationobject:nil];
}
#pragma mark -
#pragma mark Responding to keyboard events
- (void)keyboardWillShow:(NSNotification *)notification
{
/*
Reduce the size of the text view so that it's not obscured by the keyboard.
Animate the resize so that it's in sync with the appearance of the keyboard.
*/
NSDictionary *userInfo = [notificationuserInfo];
// Get the origin of the keyboard when it's displayed.
NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];
// Get the top of the keyboard as the y coordinate of its origin in self's view's coordinate system. The bottom of the text view's frame should align with the top of the keyboard's final position.
CGRect keyboardRect = [aValueCGRectValue];
// Get the duration of the animation.
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValuegetValue:&animationDuration];
// Animate the resize of the text view's frame in sync with the keyboard's appearance.
if ( [self.viewisKindOfClass:[UIScrollViewclass]] )
{
UIInterfaceOrientation interfaceOrientation = [UIApplicationsharedApplication].statusBarOrientation;
if (interfaceOrientation==UIInterfaceOrientationPortrait || interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
self.keyboardHeight = keyboardRect.size.height;
}
else
self.keyboardHeight = keyboardRect.size.width;
UIScrollView *scrollView = (UIScrollView*)self.view;
CGSize contentSize =CGSizeMake(scrollView.bounds.size.width,scrollView.bounds.size.height+self.keyboardHeight);
scrollView.contentSize =contentSize;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect =self.view.bounds;
aRect.size.height -=self.keyboardHeight;
CGPoint pt =self.activeTextField.frame.origin;
if ( pt.y+self.activeTextField.frame.size.height>(aRect.origin.y+aRect.size.height) )
{
CGPoint scrollPoint =CGPointMake(0.0, pt.y+self.activeTextField.frame.size.height-(aRect.origin.y+aRect.size.height));
[scrollViewsetContentOffset:scrollPointanimated:YES];
}
}
}
- (void)keyboardWillHide:(NSNotification *)notification
{
NSDictionary* userInfo = [notificationuserInfo];
/*
Restore the size of the text view (fill self's view).
Animate the resize so that it's in sync with the disappearance of the keyboard.
*/
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValuegetValue:&animationDuration];
if ( [self.viewisKindOfClass:[UIScrollViewclass]] )
{
UIScrollView *scrollView = (UIScrollView*)self.view;
CGSize contentSize = scrollView.bounds.size;
scrollView.contentSize =contentSize;
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
returnYES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
self.activeTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
self.activeTextField =nil;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.activeTextField =nil;
returnNO;
}
- (IBAction)onEditDone:(id)sender
{
if (self.activeTextField!=nil)
[self.activeTextFieldresignFirstResponder];
}
@end
- UIScrollView中UITextField文本编辑框位置随键盘的显示(隐藏)改变
- ios UIScrollView 上的UITextField 点击空白隐藏键盘
- 关于键盘出现与隐藏时调整UITextField的显示位置问题
- iOS中UITextField隐藏键盘
- ios 隐藏UITextField的键盘
- iOS UITextField位置随输入键盘动态改变 点击背景键盘消失
- UIScrollView添加手势显示和隐藏键盘
- iOS swift-UIView随键盘弹出/隐藏而改变位置
- UIView 上有多个UITextfield 快捷隐藏键盘 的3中方法
- iOS中改变uiTextField的光标起始位置
- iOS开发UITextField协议的用法,键盘的隐藏,以及视图随着键盘的出现隐藏而上下调整位置
- iOS:UITextField协议的用法,键盘的隐藏,以及视图随着键盘的出现隐藏而上下调整位置
- iOS中UITextField return键隐藏键盘
- 输入框根据键盘显示与隐藏移动位置
- iphone中键盘的显示和隐藏
- UITextField隐藏键盘的两种方法
- UITextField隐藏键盘的几种方式
- UITextField UITextView隐藏键盘的方法
- Java面试题及答案(1-60道)
- WPF 图层管理
- 情人的伤感抑郁QQ心情日志分享:我就这样简单的喜欢你
- 使用 LumiSoft.Net.POP3.Client 接收邮件
- 动态规划——矩阵连乘的问题
- UIScrollView中UITextField文本编辑框位置随键盘的显示(隐藏)改变
- 是什么阻碍了CIO在云方面的发展?那些行动者和尚未行动的人
- OpenTSDB学习
- maven常用命令行及解释
- ios开发工程师笔试基础题
- CIO可以承担CISO的工作吗?
- 动态绑定ASPxGridView选中状态
- Asp.net中获取应用程序完整Url路径的方法
- HTTP报文