├── ViewController.h ├── AppDelegate.h ├── main.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── UITextView+NDJHelper.h ├── UITextField+CHTHealper.h ├── Info.plist ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── ViewController.m ├── AppDelegate.m ├── UITextField+CHTHealper.m └── UITextView+NDJHelper.m /ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /UITextView+NDJHelper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITextView+NDJHelper.h 3 | // TransportCar 4 | // 5 | // Created by niudengjun on 2017/11/28. 6 | // Copyright © 2017年 amplity. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UITextView (NDJHelper) 12 | /** 13 | * 是否支持视图上移 14 | */ 15 | @property (nonatomic, assign) BOOL canMove; 16 | /** 17 | * 点击回收键盘、移动的视图,默认是当前控制器的view 18 | */ 19 | @property (nonatomic, strong) UIView *moveView; 20 | /** 21 | * textfield底部距离键盘顶部的距离 22 | */ 23 | @property (nonatomic, assign) CGFloat heightToKeyboard; 24 | 25 | @property (nonatomic, assign, readonly) CGFloat keyboardY; 26 | @property (nonatomic, assign, readonly) CGFloat keyboardHeight; 27 | @property (nonatomic, assign, readonly) CGFloat initialY; 28 | @property (nonatomic, assign, readonly) CGFloat totalHeight; 29 | @property (nonatomic, strong, readonly) UITapGestureRecognizer *tapGesture; 30 | @property (nonatomic, assign, readonly) BOOL hasContentOffset; 31 | @end 32 | -------------------------------------------------------------------------------- /UITextField+CHTHealper.h: -------------------------------------------------------------------------------- 1 | // 2 | // UITextField+CHTPositionChange.h 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UITextField (CHTHealper) 12 | 13 | /** 14 | * 是否支持视图上移 15 | */ 16 | @property (nonatomic, assign) BOOL canMove; 17 | /** 18 | * 点击回收键盘、移动的视图,默认是当前控制器的view 19 | */ 20 | @property (nonatomic, strong) UIView *moveView; 21 | /** 22 | * textfield底部距离键盘顶部的距离 23 | */ 24 | @property (nonatomic, assign) CGFloat heightToKeyboard; 25 | 26 | @property (nonatomic, assign, readonly) CGFloat keyboardY; 27 | @property (nonatomic, assign, readonly) CGFloat keyboardHeight; 28 | @property (nonatomic, assign, readonly) CGFloat initialY; 29 | @property (nonatomic, assign, readonly) CGFloat totalHeight; 30 | @property (nonatomic, strong, readonly) UITapGestureRecognizer *tapGesture; 31 | @property (nonatomic, assign, readonly) BOOL hasContentOffset; 32 | 33 | @end 34 | -------------------------------------------------------------------------------- /Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "UITextField+CHTHealper.h" 11 | 12 | @interface ViewController () 13 | 14 | @property (strong, nonatomic) IBOutlet UITextField *noMoveField; 15 | @property (strong, nonatomic) IBOutlet UITextField *defaultMoveField; 16 | @property (strong, nonatomic) IBOutlet UITextField *heightMoveField; 17 | 18 | @end 19 | 20 | @implementation ViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | self.noMoveField.canMove = NO; 25 | self.heightMoveField.heightToKeyboard = 100; 26 | 27 | UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 180, 200)]; 28 | view.backgroundColor = [UIColor whiteColor]; 29 | [self.view addSubview:view]; 30 | 31 | UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(0, 150, 180, 30)]; 32 | field.placeholder = @"我的父视图移动"; 33 | field.borderStyle = UITextBorderStyleRoundedRect; 34 | [view addSubview:field]; 35 | field.moveView = view; 36 | 37 | UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(190, 300, 180, 200)]; 38 | scrollView.backgroundColor = [UIColor whiteColor]; 39 | [self.view addSubview:scrollView]; 40 | 41 | UITextField *field2 = [[UITextField alloc] initWithFrame:CGRectMake(0, 150, 180, 30)]; 42 | field2.placeholder = @"我的父视图偏移"; 43 | field2.borderStyle = UITextBorderStyleRoundedRect; 44 | [scrollView addSubview:field2]; 45 | field2.moveView = scrollView; 46 | // Do any additional setup after loading the view, typically from a nib. 47 | } 48 | 49 | - (void)didReceiveMemoryWarning { 50 | [super didReceiveMemoryWarning]; 51 | // Dispose of any resources that can be recreated. 52 | } 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /UITextField+CHTHealper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITextField+CHTPositionChange.m 3 | // CHTTextFieldHealper 4 | // 5 | // Created by risenb_mac on 16/8/17. 6 | // Copyright © 2016年 risenb_mac. All rights reserved. 7 | // 8 | 9 | #import "UITextField+CHTHealper.h" 10 | #import 11 | 12 | static char canMoveKey; 13 | static char moveViewKey; 14 | static char heightToKeyboardKey; 15 | static char initialYKey; 16 | static char tapGestureKey; 17 | static char keyboardYKey; 18 | static char totalHeightKey; 19 | static char keyboardHeightKey; 20 | static char hasContentOffsetKey; 21 | 22 | @implementation UITextField (CHTHealper) 23 | @dynamic canMove; 24 | @dynamic moveView; 25 | @dynamic heightToKeyboard; 26 | @dynamic initialY; 27 | @dynamic tapGesture; 28 | @dynamic keyboardY; 29 | @dynamic totalHeight; 30 | @dynamic keyboardHeight; 31 | @dynamic hasContentOffset; 32 | 33 | + (void)load { 34 | static dispatch_once_t onceToken; 35 | dispatch_once(&onceToken, ^{ 36 | SEL systemSel = @selector(initWithFrame:); 37 | SEL mySel = @selector(setupInitWithFrame:); 38 | [self exchangeSystemSel:systemSel bySel:mySel]; 39 | 40 | SEL systemSel2 = @selector(becomeFirstResponder); 41 | SEL mySel2 = @selector(newBecomeFirstResponder); 42 | [self exchangeSystemSel:systemSel2 bySel:mySel2]; 43 | 44 | SEL systemSel3 = @selector(resignFirstResponder); 45 | SEL mySel3 = @selector(newResignFirstResponder); 46 | [self exchangeSystemSel:systemSel3 bySel:mySel3]; 47 | 48 | SEL systemSel4 = @selector(initWithCoder:); 49 | SEL mySel4 = @selector(setupInitWithCoder:); 50 | [self exchangeSystemSel:systemSel4 bySel:mySel4]; 51 | }); 52 | [super load]; 53 | } 54 | 55 | // 交换方法 56 | + (void)exchangeSystemSel:(SEL)systemSel bySel:(SEL)mySel { 57 | Method systemMethod = class_getInstanceMethod([self class], systemSel); 58 | Method myMethod = class_getInstanceMethod([self class], mySel); 59 | //首先动态添加方法,实现是被交换的方法,返回值表示添加成功还是失败 60 | BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(myMethod), method_getTypeEncoding(myMethod)); 61 | if (isAdd) { 62 | //如果成功,说明类中不存在这个方法的实现 63 | //将被交换方法的实现替换到这个并不存在的实现 64 | class_replaceMethod(self, mySel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod)); 65 | }else{ 66 | //否则,交换两个方法的实现 67 | method_exchangeImplementations(systemMethod, myMethod); 68 | } 69 | } 70 | 71 | - (instancetype)setupInitWithCoder:(NSCoder *)aDecoder { 72 | [self setup]; 73 | return [self setupInitWithCoder:aDecoder]; 74 | } 75 | 76 | - (instancetype)setupInitWithFrame:(CGRect)frame { 77 | [self setup]; 78 | return [self setupInitWithFrame:frame]; 79 | } 80 | 81 | - (void)setup { 82 | self.heightToKeyboard = 10; 83 | self.canMove = YES; 84 | self.keyboardY = 0; 85 | self.totalHeight = 0; 86 | self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 87 | } 88 | 89 | - (void)showAction:(NSNotification *)sender { 90 | if (!self.canMove) { 91 | return; 92 | } 93 | self.keyboardY = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; 94 | self.keyboardHeight = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; 95 | [self keyboardDidShow]; 96 | } 97 | 98 | - (void)hideAction:(NSNotification *)sender { 99 | if (!self.canMove || self.keyboardY == 0) { 100 | return; 101 | } 102 | [self hideKeyBoard:0.25]; 103 | } 104 | 105 | - (void)keyboardDidShow { 106 | if (self.keyboardHeight == 0) { 107 | return; 108 | } 109 | CGFloat fieldYInWindow = [self convertPoint:self.bounds.origin toView:[UIApplication sharedApplication].keyWindow].y; 110 | CGFloat height = (fieldYInWindow + self.heightToKeyboard + self.frame.size.height) - self.keyboardY; 111 | CGFloat moveHeight = height > 0 ? height : 0; 112 | 113 | [UIView animateWithDuration:0.25 animations:^{ 114 | if (self.hasContentOffset) { 115 | UIScrollView *scrollView = (UIScrollView *)self.moveView; 116 | scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y + moveHeight); 117 | } else { 118 | CGRect rect = self.moveView.frame; 119 | self.initialY = rect.origin.y; 120 | rect.origin.y -= moveHeight; 121 | self.moveView.frame = rect; 122 | } 123 | self.totalHeight += moveHeight; 124 | }]; 125 | } 126 | 127 | - (void)hideKeyBoard:(CGFloat)duration { 128 | [UIView animateWithDuration:duration animations:^{ 129 | if (self.hasContentOffset) { 130 | UIScrollView *scrollView = (UIScrollView *)self.moveView; 131 | scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y - self.totalHeight); 132 | } else { 133 | CGRect rect = self.moveView.frame; 134 | rect.origin.y += self.totalHeight; 135 | self.moveView.frame = rect; 136 | } 137 | self.totalHeight = 0; 138 | }]; 139 | } 140 | 141 | - (BOOL)newBecomeFirstResponder { 142 | if (self.moveView == nil) { 143 | self.moveView = [self viewController].view; 144 | } 145 | if (![self.moveView.gestureRecognizers containsObject:self.tapGesture]) { 146 | [self.moveView addGestureRecognizer:self.tapGesture]; 147 | } 148 | if ([self isFirstResponder] || !self.canMove) { 149 | return [self newBecomeFirstResponder]; 150 | } 151 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAction:) name:UIKeyboardWillShowNotification object:nil]; 152 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideAction:) name:UIKeyboardWillHideNotification object:nil]; 153 | return [self newBecomeFirstResponder]; 154 | } 155 | 156 | - (BOOL)newResignFirstResponder { 157 | if ([self.moveView.gestureRecognizers containsObject:self.tapGesture]) { 158 | [self.moveView removeGestureRecognizer:self.tapGesture]; 159 | } 160 | if (!self.canMove) { 161 | return [self newResignFirstResponder]; 162 | } 163 | BOOL result = [self newResignFirstResponder]; 164 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 165 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 166 | [self hideKeyBoard:0]; 167 | return result; 168 | } 169 | 170 | - (void)tapAction { 171 | [[self viewController].view endEditing:YES]; 172 | } 173 | 174 | - (UIViewController *)viewController { 175 | UIView *next = self; 176 | while (1) { 177 | UIResponder *nextResponder = [next nextResponder]; 178 | if ([nextResponder isKindOfClass:[UIViewController class]]) { 179 | return (UIViewController *)nextResponder; 180 | }else if ([nextResponder isKindOfClass:[UIWindow class]]){ 181 | return nil; 182 | } 183 | next = next.superview; 184 | } 185 | return nil; 186 | } 187 | 188 | - (void)setCanMove:(BOOL)canMove { 189 | objc_setAssociatedObject(self, &canMoveKey, @(canMove), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 190 | } 191 | 192 | - (BOOL)canMove { 193 | return [objc_getAssociatedObject(self, &canMoveKey) boolValue]; 194 | } 195 | 196 | - (void)setHeightToKeyboard:(CGFloat)heightToKeyboard { 197 | objc_setAssociatedObject(self, &heightToKeyboardKey, @(heightToKeyboard), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 198 | } 199 | 200 | - (CGFloat)heightToKeyboard { 201 | return [objc_getAssociatedObject(self, &heightToKeyboardKey) floatValue]; 202 | } 203 | 204 | - (void)setMoveView:(UIView *)moveView { 205 | self.hasContentOffset = NO; 206 | if ([moveView isKindOfClass:[UIScrollView class]]) { 207 | self.hasContentOffset = YES; 208 | } 209 | 210 | objc_setAssociatedObject(self, &moveViewKey, moveView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 211 | } 212 | 213 | - (UIView *)moveView { 214 | return objc_getAssociatedObject(self, &moveViewKey); 215 | } 216 | 217 | - (void)setInitialY:(CGFloat)initialY { 218 | objc_setAssociatedObject(self, &initialYKey, @(initialY), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 219 | } 220 | 221 | - (CGFloat)initialY { 222 | return [objc_getAssociatedObject(self, &initialYKey) floatValue]; 223 | } 224 | 225 | - (void)setTapGesture:(UITapGestureRecognizer *)tapGesture { 226 | objc_setAssociatedObject(self, &tapGestureKey, tapGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 227 | } 228 | 229 | - (UITapGestureRecognizer *)tapGesture { 230 | return objc_getAssociatedObject(self, &tapGestureKey); 231 | } 232 | 233 | - (void)setKeyboardY:(CGFloat)keyboardY { 234 | objc_setAssociatedObject(self, &keyboardYKey, @(keyboardY), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 235 | } 236 | 237 | - (CGFloat)keyboardY { 238 | return [objc_getAssociatedObject(self, &keyboardYKey) floatValue]; 239 | } 240 | 241 | - (void)setTotalHeight:(CGFloat)totalHeight { 242 | objc_setAssociatedObject(self, &totalHeightKey, @(totalHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 243 | } 244 | 245 | - (CGFloat)totalHeight { 246 | return [objc_getAssociatedObject(self, &totalHeightKey) floatValue]; 247 | } 248 | 249 | - (void)setKeyboardHeight:(CGFloat)keyboardHeight { 250 | objc_setAssociatedObject(self, &keyboardHeightKey, @(keyboardHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 251 | } 252 | 253 | - (CGFloat)keyboardHeight { 254 | return [objc_getAssociatedObject(self, &keyboardHeightKey) floatValue]; 255 | } 256 | 257 | - (void)setHasContentOffset:(BOOL)hasContentOffset { 258 | objc_setAssociatedObject(self, &hasContentOffsetKey, @(hasContentOffset), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 259 | } 260 | 261 | - (BOOL)hasContentOffset { 262 | return [objc_getAssociatedObject(self, &hasContentOffsetKey) boolValue]; 263 | } 264 | 265 | @end 266 | -------------------------------------------------------------------------------- /UITextView+NDJHelper.m: -------------------------------------------------------------------------------- 1 | // 2 | // UITextView+NDJHelper.m 3 | // TransportCar 4 | // 5 | // Created by niudengjun on 2017/11/28. 6 | // Copyright © 2017年 amplity. All rights reserved. 7 | // 8 | 9 | #import "UITextView+NDJHelper.h" 10 | #import 11 | static char canMoveKey; 12 | static char moveViewKey; 13 | static char heightToKeyboardKey; 14 | static char initialYKey; 15 | static char tapGestureKey; 16 | static char keyboardYKey; 17 | static char totalHeightKey; 18 | static char keyboardHeightKey; 19 | static char hasContentOffsetKey; 20 | 21 | @implementation UITextView (NDJHelper) 22 | @dynamic canMove; 23 | @dynamic moveView; 24 | @dynamic heightToKeyboard; 25 | @dynamic initialY; 26 | @dynamic tapGesture; 27 | @dynamic keyboardY; 28 | @dynamic totalHeight; 29 | @dynamic keyboardHeight; 30 | @dynamic hasContentOffset; 31 | 32 | + (void)load { 33 | 34 | static dispatch_once_t onceToken; 35 | dispatch_once(&onceToken, ^{ 36 | 37 | if(IOS_VERSION >=9){ 38 | SEL systemSel = @selector(initWithFrame:); 39 | SEL mySel = @selector(setupInitWithFrame:); 40 | [self exchangeSystemSel:systemSel bySel:mySel]; 41 | 42 | SEL systemSel2 = @selector(becomeFirstResponder); 43 | SEL mySel2 = @selector(newBecomeFirstResponder); 44 | [self exchangeSystemSel:systemSel2 bySel:mySel2]; 45 | 46 | SEL systemSel3 = @selector(resignFirstResponder); 47 | SEL mySel3 = @selector(newResignFirstResponder); 48 | [self exchangeSystemSel:systemSel3 bySel:mySel3]; 49 | 50 | SEL systemSel4 = @selector(initWithCoder:); 51 | SEL mySel4 = @selector(setupInitWithCoder:); 52 | [self exchangeSystemSel:systemSel4 bySel:mySel4]; 53 | } 54 | }); 55 | } 56 | 57 | // 交换方法 58 | + (void)exchangeSystemSel:(SEL)systemSel bySel:(SEL)mySel { 59 | Method systemMethod = class_getInstanceMethod([self class], systemSel); 60 | Method myMethod = class_getInstanceMethod([self class], mySel); 61 | //首先动态添加方法,实现是被交换的方法,返回值表示添加成功还是失败 62 | BOOL isAdd = class_addMethod(self, systemSel, method_getImplementation(myMethod), method_getTypeEncoding(myMethod)); 63 | if (isAdd) { 64 | //如果成功,说明类中不存在这个方法的实现 65 | //将被交换方法的实现替换到这个并不存在的实现 66 | class_replaceMethod(self, mySel, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod)); 67 | }else{ 68 | //否则,交换两个方法的实现 69 | method_exchangeImplementations(systemMethod, myMethod); 70 | } 71 | } 72 | 73 | - (instancetype)setupInitWithCoder:(NSCoder *)aDecoder { 74 | [self setup]; 75 | return [self setupInitWithCoder:aDecoder]; 76 | } 77 | 78 | - (instancetype)setupInitWithFrame:(CGRect)frame { 79 | [self setup]; 80 | return [self setupInitWithFrame:frame]; 81 | } 82 | 83 | - (void)setup { 84 | self.heightToKeyboard = 10; 85 | self.canMove = YES; 86 | self.keyboardY = 0; 87 | self.totalHeight = 0; 88 | self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)]; 89 | } 90 | 91 | - (void)showAction:(NSNotification *)sender { 92 | NSLog(@"%@", sender); 93 | if (!self.canMove) { 94 | return; 95 | } 96 | self.keyboardY = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; 97 | self.keyboardHeight = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height; 98 | [self keyboardDidShow]; 99 | } 100 | 101 | - (void)hideAction:(NSNotification *)sender { 102 | if (!self.canMove || self.keyboardY == 0) { 103 | return; 104 | } 105 | [self hideKeyBoard:0.25]; 106 | } 107 | 108 | - (void)keyboardDidShow { 109 | if (self.keyboardHeight == 0) { 110 | return; 111 | } 112 | CGFloat fieldYInWindow = [self convertPoint:self.bounds.origin toView:[UIApplication sharedApplication].keyWindow].y; 113 | CGFloat height = (fieldYInWindow + self.heightToKeyboard + self.frame.size.height) - self.keyboardY; 114 | CGFloat moveHeight = height > 0 ? height : 0; 115 | 116 | [UIView animateWithDuration:0.25 animations:^{ 117 | if (self.hasContentOffset) { 118 | UIScrollView *scrollView = (UIScrollView *)self.moveView; 119 | scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y + moveHeight); 120 | } else { 121 | CGRect rect = self.moveView.frame; 122 | self.initialY = rect.origin.y; 123 | rect.origin.y -= moveHeight; 124 | self.moveView.frame = rect; 125 | } 126 | self.totalHeight += moveHeight; 127 | }]; 128 | } 129 | 130 | - (void)hideKeyBoard:(CGFloat)duration { 131 | [UIView animateWithDuration:duration animations:^{ 132 | if (self.hasContentOffset) { 133 | UIScrollView *scrollView = (UIScrollView *)self.moveView; 134 | scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, scrollView.contentOffset.y - self.totalHeight); 135 | } else { 136 | CGRect rect = self.moveView.frame; 137 | rect.origin.y += self.totalHeight; 138 | self.moveView.frame = rect; 139 | } 140 | self.totalHeight = 0; 141 | }]; 142 | } 143 | 144 | - (BOOL)newBecomeFirstResponder { 145 | if (self.moveView == nil) { 146 | self.moveView = [self viewController].view; 147 | } 148 | // if (![self.moveView.gestureRecognizers containsObject:self.tapGesture]) { 149 | // [self.moveView addGestureRecognizer:self.tapGesture]; 150 | // } 151 | if ([self isFirstResponder] || !self.canMove) { 152 | return [self newBecomeFirstResponder]; 153 | } 154 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showAction:) name:UIKeyboardWillShowNotification object:nil]; 155 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideAction:) name:UIKeyboardWillHideNotification object:nil]; 156 | return [self newBecomeFirstResponder]; 157 | } 158 | 159 | - (BOOL)newResignFirstResponder { 160 | if ([self.moveView.gestureRecognizers containsObject:self.tapGesture]) { 161 | [self.moveView removeGestureRecognizer:self.tapGesture]; 162 | } 163 | if (!self.canMove) { 164 | return [self newResignFirstResponder]; 165 | } 166 | BOOL result = [self newResignFirstResponder]; 167 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 168 | [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 169 | [self hideKeyBoard:0]; 170 | return result; 171 | } 172 | 173 | - (void)tapAction { 174 | [[self viewController].view endEditing:YES]; 175 | } 176 | 177 | - (UIViewController *)viewController { 178 | UIView *next = self; 179 | while (1) { 180 | UIResponder *nextResponder = [next nextResponder]; 181 | if ([nextResponder isKindOfClass:[UIViewController class]]) { 182 | return (UIViewController *)nextResponder; 183 | }else if ([nextResponder isKindOfClass:[UIWindow class]]){ 184 | return nil; 185 | } 186 | next = next.superview; 187 | } 188 | return nil; 189 | } 190 | 191 | - (void)setCanMove:(BOOL)canMove { 192 | objc_setAssociatedObject(self, &canMoveKey, @(canMove), OBJC_ASSOCIATION_ASSIGN); 193 | } 194 | 195 | - (BOOL)canMove { 196 | return [objc_getAssociatedObject(self, &canMoveKey) boolValue]; 197 | } 198 | 199 | - (void)setHeightToKeyboard:(CGFloat)heightToKeyboard { 200 | objc_setAssociatedObject(self, &heightToKeyboardKey, @(heightToKeyboard), OBJC_ASSOCIATION_RETAIN); 201 | } 202 | 203 | - (CGFloat)heightToKeyboard { 204 | return [objc_getAssociatedObject(self, &heightToKeyboardKey) floatValue]; 205 | } 206 | 207 | - (void)setMoveView:(UIView *)moveView { 208 | self.hasContentOffset = NO; 209 | if ([moveView isKindOfClass:[UIScrollView class]]) { 210 | self.hasContentOffset = YES; 211 | } 212 | 213 | objc_setAssociatedObject(self, &moveViewKey, moveView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 214 | } 215 | 216 | - (UIView *)moveView { 217 | return objc_getAssociatedObject(self, &moveViewKey); 218 | } 219 | 220 | - (void)setInitialY:(CGFloat)initialY { 221 | objc_setAssociatedObject(self, &initialYKey, @(initialY), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 222 | } 223 | 224 | - (CGFloat)initialY { 225 | return [objc_getAssociatedObject(self, &initialYKey) floatValue]; 226 | } 227 | 228 | - (void)setTapGesture:(UITapGestureRecognizer *)tapGesture { 229 | objc_setAssociatedObject(self, &tapGestureKey, tapGesture, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 230 | } 231 | 232 | - (UITapGestureRecognizer *)tapGesture { 233 | return objc_getAssociatedObject(self, &tapGestureKey); 234 | } 235 | 236 | - (void)setKeyboardY:(CGFloat)keyboardY { 237 | objc_setAssociatedObject(self, &keyboardYKey, @(keyboardY), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 238 | } 239 | 240 | - (CGFloat)keyboardY { 241 | return [objc_getAssociatedObject(self, &keyboardYKey) floatValue]; 242 | } 243 | 244 | - (void)setTotalHeight:(CGFloat)totalHeight { 245 | objc_setAssociatedObject(self, &totalHeightKey, @(totalHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 246 | } 247 | 248 | - (CGFloat)totalHeight { 249 | return [objc_getAssociatedObject(self, &totalHeightKey) floatValue]; 250 | } 251 | 252 | - (void)setKeyboardHeight:(CGFloat)keyboardHeight { 253 | objc_setAssociatedObject(self, &keyboardHeightKey, @(keyboardHeight), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 254 | } 255 | 256 | - (CGFloat)keyboardHeight { 257 | return [objc_getAssociatedObject(self, &keyboardHeightKey) floatValue]; 258 | } 259 | 260 | - (void)setHasContentOffset:(BOOL)hasContentOffset { 261 | objc_setAssociatedObject(self, &hasContentOffsetKey, @(hasContentOffset), OBJC_ASSOCIATION_ASSIGN); 262 | } 263 | 264 | - (BOOL)hasContentOffset { 265 | return [objc_getAssociatedObject(self, &hasContentOffsetKey) boolValue]; 266 | } 267 | 268 | @end 269 | --------------------------------------------------------------------------------