版本:6.5.5
IQKeyboardManager.h
@interface IQKeyboardManager : NSObject
//单例
+ (nonnull instancetype)sharedManager;
//启用/禁用距离功能。默认为YES
@property(nonatomic, assign, getter = isEnabled) BOOL enable;
//设置键盘与textField的距离。不能小于零。默认值为10。下图距离
@property(nonatomic, assign) CGFloat keyboardDistanceFromTextField;
//手动刷新
- (void)reloadLayoutIfNeeded;
//键盘是否显示 readonly
@property(nonatomic, assign, readonly, getter = isKeyboardShowing) BOOL keyboardShowing;
//视图上移的距离 readonly
@property(nonatomic, assign, readonly) CGFloat movedDistance;
//视图上移的距离的回调
@property(nullable, nonatomic, copy) void (^movedDistanceChanged)(CGFloat movedDistance);
//自动添加IQToolbar功能。默认为YES。
@property(nonatomic, assign, getter = isEnableAutoToolbar) BOOL enableAutoToolbar;
为YES时 自动添加下图toolbar
若要屏蔽单个输入框的toolbar功能,可以添加
self.textField.inputAccessoryView = [[UIView alloc] init];
typedef NS_ENUM(NSInteger, IQAutoToolbarManageBehaviour) {
IQAutoToolbarBySubviews,//按视图层级 默认
IQAutoToolbarByTag, //按视图的tag
IQAutoToolbarByPosition,//按视图之间的位置 上到下 左到右
};
//输入框顺序判断方式
@property(nonatomic, assign) IQAutoToolbarManageBehaviour toolbarManageBehaviour;
//如果为YES,则对IQToolbar使用textField的tintColor属性,为NO时,颜色为nil。默认为NO。
@property(nonatomic, assign) BOOL shouldToolbarUsesTextFieldTintColor;
例:
[IQKeyboardManager sharedManager].shouldToolbarUsesTextFieldTintColor = YES;
_textField.tintColor = [UIColor redColor];
效果如下图,前进后退 done都变为tintColor
//这用于toolbar.tintColor。默认值为nil。如果shouldToolbarUsesTextFieldTintColor为YES,那么将忽略此属性
@property(nullable, nonatomic, strong) UIColor *toolbarTintColor;
//这用于toolbar.barTintColor。默认值为nil。
@property(nullable, nonatomic, strong) UIColor *toolbarBarTintColor;
例:
[IQKeyboardManager sharedManager].shouldToolbarUsesTextFieldTintColor = NO;
[IQKeyboardManager sharedManager].toolbarTintColor = [UIColor redColor];
[IQKeyboardManager sharedManager].toolbarBarTintColor = [UIColor yellowColor];
效果如下图 前进后退done为toolbarTintColor,背景颜色为toolbarBarTintColor
typedef NS_ENUM(NSUInteger, IQPreviousNextDisplayMode) {
IQPreviousNextDisplayModeDefault, //默认 界面多输入框显示切换箭头,单输入框不显示
IQPreviousNextDisplayModeAlwaysHide, //一直隐藏
IQPreviousNextDisplayModeAlwaysShow, //一直显示
};
//前进后退箭头的显示方式
@property(nonatomic, assign) IQPreviousNextDisplayMode previousNextDisplayMode;
例:
[IQKeyboardManager sharedManager].previousNextDisplayMode = IQPreviousNextDisplayModeAlwaysHide;
效果如下图,前进后退按钮被隐藏
//toolbar的上一个/下一个/完成按钮的图标
@property(nullable, nonatomic, strong) UIImage *toolbarPreviousBarButtonItemImage;
@property(nullable, nonatomic, strong) UIImage *toolbarNextBarButtonItemImage;
@property(nullable, nonatomic, strong) UIImage *toolbarDoneBarButtonItemImage;
例:
[IQKeyboardManager sharedManager].toolbarPreviousBarButtonItemImage = [UIImage keyboardLeftImage];
[IQKeyboardManager sharedManager].toolbarNextBarButtonItemImage = [UIImage keyboardUpImage];
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemImage = [UIImage keyboardPreviousImage];
效果如下图
//toolbar的上一个/下一个/完成按钮的文本和读屏幕时的文本
@property(nullable, nonatomic, strong) NSString *toolbarPreviousBarButtonItemText;
@property(nullable, nonatomic, strong) NSString *toolbarPreviousBarButtonItemAccessibilityLabel;
@property(nullable, nonatomic, strong) NSString *toolbarNextBarButtonItemText;
@property(nullable, nonatomic, strong) NSString *toolbarNextBarButtonItemAccessibilityLabel;
@property(nullable, nonatomic, strong) NSString *toolbarDoneBarButtonItemText;
@property(nullable, nonatomic, strong) NSString *toolbarDoneBarButtonItemAccessibilityLabel;
//如果为YES,那么它将在IQToolbar上显示textField的Placeholder。默认为YES。
@property(nonatomic, assign) BOOL shouldShowToolbarPlaceholder;
例:
[IQKeyboardManager sharedManager].toolbarPreviousBarButtonItemText = @"-";
[IQKeyboardManager sharedManager].toolbarNextBarButtonItemText = @"+";
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemText = @"=";
[IQKeyboardManager sharedManager].shouldShowToolbarPlaceholder = NO;
效果如下图,按钮被替换,Placeholde被隐藏
//IQToolbar.Placeholder的字体。默认值为nil。使用 [UIFont systemFontOfSize:12]
@property(nullable, nonatomic, strong) UIFont *placeholderFont;
//IQToolbar.Placeholder的颜色。默认值为nil。使用 lightGray
@property(nullable, nonatomic, strong) UIColor *placeholderColor;
例:
[IQKeyboardManager sharedManager].placeholderFont = [UIFont systemFontOfSize:18];
[IQKeyboardManager sharedManager].placeholderColor = [UIColor orangeColor];
效果如下图,
//IQToolbar.Placeholder按钮的颜色,当它被视为按钮时。默认为nil
@property(nullable, nonatomic, strong) UIColor *placeholderButtonColor;
例:
[IQKeyboardManager sharedManager].placeholderButtonColor = [UIColor cyanColor];
//需要添加target才会将placeholder视为按钮
[self.textField1.keyboardToolbar.titleBarButton setTarget:self action:@selector(controlAction:)];
- (void)controlAction:(id)sender {
if ([sender isEqual:[self.textField1.keyboardToolbar.titleBarButton valueForKey:@"titleButton"]]) {
[[IQKeyboardManager sharedManager] resignFirstResponder];
}
}
效果如下 此时按钮可以点击 并执行controlAction:方法
//重新刷新加载所有工具栏按钮。
- (void)reloadInputViews;
//覆盖所有textField/textView的keyboardAppearance。默认为NO
//此时根据UITraitCollection.currentTraitCollection.userInterfaceStyle来显示
@property(nonatomic, assign) BOOL overrideKeyboardAppearance;
typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault, //默认状态 黑暗模式时使用黑暗模式的UI 明亮模式则使用明亮模式的UI
UIKeyboardAppearanceDark //一直使用黑暗模式的UI
UIKeyboardAppearanceLight //一直使用明亮模式的UI
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated
};
//如果overrideKeyboardAppearance为YES,则使用此属性设置所有textField的keyboardAppearance。
@property(nonatomic, assign) UIKeyboardAppearance keyboardAppearance;
例:
[IQKeyboardManager sharedManager].overrideKeyboardAppearance = YES;
[IQKeyboardManager sharedManager].keyboardAppearance = UIKeyboardAppearanceDark;
效果如下,不管手机是否黑暗模式,键盘一直为UIKeyboardAppearanceDark
//键盘弹出的情况下点击键盘外部则可退出键盘。默认为NO
@property(nonatomic, assign) BOOL shouldResignOnTouchOutside;
//TapGesture使视图上的触摸退出键盘。readonly
@property(nonnull, nonatomic, strong, readonly) UITapGestureRecognizer *resignFirstResponderGesture;
//退出第一响应者
- (BOOL)resignFirstResponder;
//如果可以导航到先前的响应者textField / textView,则返回YES,否则返回NO。
@property (nonatomic, readonly) BOOL canGoPrevious;
//如果可以导航到下一个的响应者textField / textView,则返回YES,否则返回NO。
@property (nonatomic, readonly) BOOL canGoNext;
//导航到先前的响应者
- (BOOL)goPrevious;
//导航到下一个的响应者
- (BOOL)goNext;
例:
[self.textField1 addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(controlAction:)
nextAction:@selector(controlAction:) doneAction:@selector(controlAction:)];
- (void)controlAction:(id)sender {
if ([sender isEqual:self.textField1.keyboardToolbar.previousBarButton]) {
if ([IQKeyboardManager sharedManager].canGoPrevious) {
[[IQKeyboardManager sharedManager] goPrevious];
}
} else if ([sender isEqual:self.textField1.keyboardToolbar.nextBarButton]) {
if ([IQKeyboardManager sharedManager].canGoNext) {
[[IQKeyboardManager sharedManager] goNext];
}
} else if ([sender isEqual:self.textField1.keyboardToolbar.doneBarButton]) {
[[IQKeyboardManager sharedManager] resignFirstResponder];
}
}
//如果为YES,则它将在下一个/上一个/完成的单击时播放inputClick声音([[UIDevice currentDevice] playInputClick])。默认为YES。
@property(nonatomic, assign) BOOL shouldPlayInputClicks;
//如果为YES,则在对viewController的视图进行任何frame更新时都会调用setNeedsLayout layoutIfNeeded
//[view setNeedsLayout];
//[view layoutIfNeeded];
@property(nonatomic, assign) BOOL layoutIfNeededOnUpdate;
//其中的类强制 enable = NO,类都应该是一种UIViewController,默认有 [UITableViewController, UIAlertController, _UIAlertControllerTextFieldViewController]
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *disabledDistanceHandlingClasses;
//其中的类强制 enable = YES,类都应该是一种UIViewController默认有 []
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *enabledDistanceHandlingClasses;
//其中的类强制 enableAutoToolbar = NO,类都应该是一种UIViewController,默认有 [UIAlertController, _UIAlertControllerTextFieldViewController].
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *disabledToolbarClasses;
//其中的类强制 enableAutoToolbar = YES,类都应该是一种UIViewController,默认有 [].
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *enabledToolbarClasses;
//其中的类 能导航到另一个响应者,类都应该是一种UIView 默认有 [UITableView, UICollectionView, IQPreviousNextView].
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *toolbarPreviousNextAllowedClasses;
//其中的类强制 shouldResignOnTouchOutside = NO,类都应该是一种UIViewController,默认有 [UIAlertController, UIAlertControllerTextFieldViewController]
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *disabledTouchResignedClasses;
//其中的类强制 shouldResignOnTouchOutside = YES,类都应该是一种UIViewController, 默认有 []
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *enabledTouchResignedClasses;
//如果 shouldResignOnTouchOutside = YES,则可以在某些特定的视图子类上不识别手势触摸。类应该是一种UIView。默认有 [UIControl,UINavigationBar]
@property(nonatomic, strong, nonnull, readonly) NSMutableSet *touchResignedGestureIgnoreClasses;
例:
[[IQKeyboardManager sharedManager].disabledDistanceHandlingClasses addObject:[self class]];
或
[[IQKeyboardManager sharedManager].disabledDistanceHandlingClasses removeObject:[self class]];
//是否输出日志
@property(nonatomic, assign) BOOL enableDebugging;
// enable = YES 时,unregisterAllNotifications可实现enable = NO的功能
-(void)registerAllNotifications;
-(void)unregisterAllNotifications;
例:
[IQKeyboardManager sharedManager].enable = NO;
等同于
[IQKeyboardManager sharedManager].enable = YES
[[IQKeyboardManager sharedManager] unregisterAllNotifications];
//不可使用下面方式初始化
- (nonnull instancetype)init NS_UNAVAILABLE;
+ (nonnull instancetype)new NS_UNAVAILABLE;
@end
IQUITextFieldView+Additions.h
//设置单个输入框的功能
@interface UIView (Additions)
//设置键盘与textField的距离。不能小于零。默认值为10。
@property(nonatomic, assign) CGFloat keyboardDistanceFromTextField;
//如果ignoreSwitchingByNextPrevious为YES,则库将忽略此textField/textView
//同时使用键盘工具栏上一个下一个按钮移至其他textField / textView。默认为NO
@property(nonatomic, assign) BOOL ignoreSwitchingByNextPrevious;
typedef NS_ENUM(NSUInteger, IQEnableMode) {
IQEnableModeDefault, //默认 此状态下才判断IQKeyboardManager的其他使能属性
IQEnableModeEnabled, //打开 忽略IQKeyboardManager的其他使能属性
IQEnableModeDisabled, //关闭 忽略IQKeyboardManager的其他使能属性
};
//使能模式 最强的权限
//enableMode>disabledDistanceHandlingClasses>enabledDistanceHandlingClasses>IQKeyboardManager.enable
@property(nonatomic, assign) IQEnableMode enableMode;
//使能模式 最强的权限
//shouldResignOnTouchOutsideMode>disabledTouchResignedClasses>enabledTouchResignedClasses>IQKeyboardManager.shouldResignOnTouchOutsideMode
@property(nonatomic, assign) IQEnableMode shouldResignOnTouchOutsideMode;
@end
例:
self.textField.keyboardDistanceFromTextField = 100;
self.textField.ignoreSwitchingByNextPrevious = YES;
self.textField.enableMode = IQEnableModeEnabled;
self.textField.shouldResignOnTouchOutsideMode = IQEnableModeDisabled;
效果如下图,输入框离键盘距离100 切换到其他输入框后不能点箭头返回,也不能点屏幕退出键盘
IQUIView+IQKeyboardToolbar.h
@interface UIImage (IQKeyboardToolbarNextPreviousImage)
//获取IQKeyboard自带的各类图片
+(nullable UIImage*)keyboardLeftImage;
+(nullable UIImage*)keyboardRightImage;
+(nullable UIImage*)keyboardUpImage;
+(nullable UIImage*)keyboardDownImage;
+(nullable UIImage*)keyboardPreviousImage;
+(nullable UIImage*)keyboardNextImage;
@end
例:
[IQKeyboardManager sharedManager].toolbarPreviousBarButtonItemImage = [UIImage keyboardLeftImage];
[IQKeyboardManager sharedManager].toolbarNextBarButtonItemImage = [UIImage keyboardUpImage];
[IQKeyboardManager sharedManager].toolbarDoneBarButtonItemImage = [UIImage keyboardPreviousImage];
@interface UIView (IQToolbarAddition)
//获取键盘的IQToolbar
@property (readonly, nonatomic, nonnull) IQToolbar *keyboardToolbar;
//如果shouldHideToolbarPlaceholder为YES,则标题将不会添加到工具栏。默认为NO。
@property (assign, nonatomic) BOOL shouldHideToolbarPlaceholder;
例:
self.textField1.shouldHideToolbarPlaceholder = YES;
效果如下,标题不显示
//在工具栏上绘制Placeholder时,toolbarPlaceholder会覆盖默认的placeholder文本。
@property (nullable, strong, nonatomic) NSString* toolbarPlaceholder;
//`drawingToolbarPlaceholder`将是用于在工具栏上绘制的实际文本 readonly
@property (nullable, strong, nonatomic, readonly) NSString* drawingToolbarPlaceholder;
例:
self.textField1.toolbarPlaceholder = @"self.textField1.toolbarPlaceholder";
效果如下
//重新创建toolbar
- (void)addKeyboardToolbarWithTarget:(nullable id)target titleText:(nullable NSString*)titleText
rightBarButtonConfiguration:(nullable IQBarButtonItemConfiguration*)rightBarButtonConfiguration
previousBarButtonConfiguration:(nullable IQBarButtonItemConfiguration*)previousBarButtonConfiguration
nextBarButtonConfiguration:(nullable IQBarButtonItemConfiguration*)nextBarButtonConfiguration;
//以下的所有方法都将会最终调用该方法
例:
[self.textField1 addKeyboardToolbarWithTarget:self titleText:@"textField1"
rightBarButtonConfiguration:[[IQBarButtonItemConfiguration alloc] initWithTitle:@"完成" action:@selector(controlAction:)]
previousBarButtonConfiguration:nil nextBarButtonConfiguration:nil];
效果如下
///------------
/// @name Done
///------------
- (void)addDoneOnKeyboardWithTarget:(nullable id)target action:(nullable SEL)action;
- (void)addDoneOnKeyboardWithTarget:(nullable id)target action:(nullable SEL)action shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addDoneOnKeyboardWithTarget:(nullable id)target action:(nullable SEL)action titleText:(nullable NSString*)titleText;
///------------
/// @name Right
///------------
- (void)addRightButtonOnKeyboardWithText:(nullable NSString*)text target:(nullable id)target action:(nullable SEL)action;
- (void)addRightButtonOnKeyboardWithText:(nullable NSString*)text target:(nullable id)target action:(nullable SEL)action shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addRightButtonOnKeyboardWithText:(nullable NSString*)text target:(nullable id)target action:(nullable SEL)action titleText:(nullable NSString*)titleText;
- (void)addRightButtonOnKeyboardWithImage:(nullable UIImage*)image target:(nullable id)target action:(nullable SEL)action;
- (void)addRightButtonOnKeyboardWithImage:(nullable UIImage*)image target:(nullable id)target action:(nullable SEL)action shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addRightButtonOnKeyboardWithImage:(nullable UIImage*)image target:(nullable id)target action:(nullable SEL)action titleText:(nullable NSString*)titleText;
///------------------
/// @name Cancel/Done
///------------------
- (void)addCancelDoneOnKeyboardWithTarget:(nullable id)target cancelAction:(nullable SEL)cancelAction doneAction:(nullable SEL)doneAction;
- (void)addCancelDoneOnKeyboardWithTarget:(nullable id)target cancelAction:(nullable SEL)cancelAction doneAction:(nullable SEL)doneAction shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addCancelDoneOnKeyboardWithTarget:(nullable id)target cancelAction:(nullable SEL)cancelAction doneAction:(nullable SEL)doneAction titleText:(nullable NSString*)titleText;
///-----------------
/// @name Right/Left
///-----------------
- (void)addLeftRightOnKeyboardWithTarget:(nullable id)target leftButtonTitle:(nullable NSString*)leftButtonTitle rightButtonTitle:(nullable NSString*)rightButtonTitle leftButtonAction:(nullable SEL)leftButtonAction rightButtonAction:(nullable SEL)rightButtonAction;
- (void)addLeftRightOnKeyboardWithTarget:(nullable id)target leftButtonTitle:(nullable NSString*)leftButtonTitle rightButtonTitle:(nullable NSString*)rightButtonTitle leftButtonAction:(nullable SEL)leftButtonAction rightButtonAction:(nullable SEL)rightButtonAction shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addLeftRightOnKeyboardWithTarget:(nullable id)target leftButtonTitle:(nullable NSString*)leftButtonTitle rightButtonTitle:(nullable NSString*)rightButtonTitle leftButtonAction:(nullable SEL)leftButtonAction rightButtonAction:(nullable SEL)rightButtonAction titleText:(nullable NSString*)titleText;
///-------------------------
/// @name Previous/Next/Done
///-------------------------
- (void)addPreviousNextDoneOnKeyboardWithTarget:(nullable id)target previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction doneAction:(nullable SEL)doneAction;
- (void)addPreviousNextDoneOnKeyboardWithTarget:(nullable id)target previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction doneAction:(nullable SEL)doneAction shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addPreviousNextDoneOnKeyboardWithTarget:(nullable id)target previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction doneAction:(nullable SEL)doneAction titleText:(nullable NSString*)titleText;
///--------------------------
/// @name Previous/Next/Right
///--------------------------
- (void)addPreviousNextRightOnKeyboardWithTarget:(nullable id)target rightButtonTitle:(nullable NSString*)rightButtonTitle previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction rightButtonAction:(nullable SEL)rightButtonAction;
- (void)addPreviousNextRightOnKeyboardWithTarget:(nullable id)target rightButtonTitle:(nullable NSString*)rightButtonTitle previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction rightButtonAction:(nullable SEL)rightButtonAction shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addPreviousNextRightOnKeyboardWithTarget:(nullable id)target rightButtonTitle:(nullable NSString*)rightButtonTitle previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction rightButtonAction:(nullable SEL)rightButtonAction titleText:(nullable NSString*)titleText;
- (void)addPreviousNextRightOnKeyboardWithTarget:(nullable id)target rightButtonImage:(nullable UIImage*)rightButtonImage previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction rightButtonAction:(nullable SEL)rightButtonAction;
- (void)addPreviousNextRightOnKeyboardWithTarget:(nullable id)target rightButtonImage:(nullable UIImage*)rightButtonImage previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction rightButtonAction:(nullable SEL)rightButtonAction shouldShowPlaceholder:(BOOL)shouldShowPlaceholder;
- (void)addPreviousNextRightOnKeyboardWithTarget:(nullable id)target rightButtonImage:(nullable UIImage*)rightButtonImage previousAction:(nullable SEL)previousAction nextAction:(nullable SEL)nextAction rightButtonAction:(nullable SEL)rightButtonAction titleText:(nullable NSString*)titleText;
@end
//重新创建toolbar时使用
@interface IQBarButtonItemConfiguration : NSObject
//通过SystemItem初始化
-(nonnull instancetype)initWithBarButtonSystemItem:(UIBarButtonSystemItem)barButtonSystemItem action:(nullable SEL)action;
//通过图片初始化
-(nonnull instancetype)initWithImage:(nonnull UIImage*)image action:(nullable SEL)action;
//通过文本初始化
-(nonnull instancetype)initWithTitle:(nonnull NSString*)title action:(nullable SEL)action;
//只读
@property (readonly, nonatomic) UIBarButtonSystemItem barButtonSystemItem;
@property (readonly, nonatomic, nullable) UIImage *image;
@property (readonly, nonatomic, nullable) NSString *title;
@property (readonly, nonatomic, nullable) SEL action;
@end
例:
[self.textField1 addKeyboardToolbarWithTarget:self titleText:@"textField1"
rightBarButtonConfiguration:[[IQBarButtonItemConfiguration alloc] initWithTitle:@"完成" action:@selector(controlAction:)]
previousBarButtonConfiguration:nil nextBarButtonConfiguration:nil];
IQToolbar.h
@interface IQToolbar : UIToolbar
//上一个
@property(nonnull, nonatomic, strong) IQBarButtonItem *previousBarButton;
//下一个
@property(nonnull, nonatomic, strong) IQBarButtonItem *nextBarButton;
//标题 只读
@property(nonnull, nonatomic, strong, readonly) IQTitleBarButtonItem *titleBarButton;
//完成
@property(nonnull, nonatomic, strong) IQBarButtonItem *doneBarButton;
//间隙
@property(nonnull, nonatomic, strong) IQBarButtonItem *fixedSpaceBarButton;
@end
例:IQBarButtonItem修改
[self.textField1 addPreviousNextDoneOnKeyboardWithTarget:self previousAction:@selector(controlAction:)
nextAction:@selector(controlAction:) doneAction:@selector(controlAction:)];
//必须重新创建toolbar后才能修改下面IQBarButtonItem的属性
self.textField1.keyboardToolbar.previousBarButton.image = [UIImage keyboardRightImage];
self.textField1.keyboardToolbar.nextBarButton.image = [UIImage keyboardLeftImage];
效果如下
例:IQTitleBarButtonItem修改
[self.textField1 addKeyboardToolbarWithTarget:self titleText:@"textField1" rightBarButtonConfiguration:[[IQBarButtonItemConfiguration alloc] initWithTitle:@"完成" action:@selector(controlAction:)] previousBarButtonConfiguration:nil nextBarButtonConfiguration:nil];
self.textField1.keyboardToolbar.titleBarButton.title = @"123";
效果如下
IQBarButtonItem.h
@interface IQBarButtonItem : UIBarButtonItem
//是否为系统item (使用initWithBarButtonSystemItem:target:action:创建的item会为YES)
@property (nonatomic, readonly) BOOL isSystemItem;
//为item添加方法 不会影响之前绑定的方法
//若textfield.keyboardToolbar.titleBarButton调用 则将标题改为按钮
-(void)setTarget:(nullable id)target action:(nullable SEL)action;
//调用方法 setTarget:action:时会自动初始化
@property (nullable, strong, nonatomic) NSInvocation *invocation;
@end
例:
[self.textField1.keyboardToolbar.titleBarButton setTarget:self action:@selector(controlAction:)];
效果如下 此时标题变为按钮,可点击
IQTitleBarButtonItem.h
@interface IQTitleBarButtonItem : IQBarButtonItem
//标题字体 默认为(system font 12.0 bold)
@property(nullable, nonatomic, strong) UIFont *titleFont;
//标题颜色
@property(nullable, nonatomic, strong) UIColor *titleColor;
//标题为按钮时的颜色(实现了setTarget:action:)
@property(nullable, nonatomic, strong) UIColor *selectableTitleColor;
//初始化 内部调用(外部调用时使用[textfield addKeyboardToolbarWithTarget:]新建toolbar)
-(nonnull instancetype)initWithTitle:(nullable NSString *)title NS_DESIGNATED_INITIALIZER;
//不可用初始化
-(nonnull instancetype)init NS_UNAVAILABLE;
-(nonnull instancetype)initWithCoder:(nullable NSCoder *)aDecoder NS_UNAVAILABLE;
+ (nonnull instancetype)new NS_UNAVAILABLE;
@end
例:
self.textField1.keyboardToolbar.titleBarButton.titleFont = [UIFont systemFontOfSize:20];
self.textField1.keyboardToolbar.titleBarButton.titleColor = [UIColor redColor];
效果如下
例:
[self.textField1.keyboardToolbar.titleBarButton setTarget:self action:@selector(controlAction:)];
self.textField1.keyboardToolbar.titleBarButton.selectableTitleColor = [UIColor blueColor];
效果如下 此时标题为按钮
IQKeyboardReturnKeyHandler.h
@interface IQKeyboardReturnKeyHandler : NSObject
//初始化
-(nonnull instancetype)initWithViewController:(nullable UIViewController*)controller NS_DESIGNATED_INITIALIZER;
//不可用初始化
-(nonnull instancetype)initWithCoder:(nullable NSCoder *)aDecoder NS_UNAVAILABLE;
//委托 可使用UITextFieldDelegate,UITextViewDelegate
@property(nullable, nonatomic, weak) id delegate;
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
UIReturnKeyContinue API_AVAILABLE(ios(9.0)),
};
//一个输入框的返回键类型。默认值为UIReturnKeyDefault。
@property(nonatomic, assign) UIReturnKeyType lastTextFieldReturnKeyType;
//添加一个新的UITextField/UITextView 的返回键类型和委托给UIViewController
-(void)addTextFieldView:(nonnull UIView*)textFieldView;
//删除
-(void)removeTextFieldView:(nonnull UIView*)textFieldView;
//添加view中所有的UITextField/UITextView 的返回键类型和委托给UIViewController
-(void)addResponderFromView:(nonnull UIView*)view;
//删除
-(void)removeResponderFromView:(nonnull UIView*)view;
@end
例:
handler必须为属性或全局变量,不然没有被持有,会被controller释放
self.handler = [[IQKeyboardReturnKeyHandler alloc] initWithViewController:self];
[self.handler setLastTextFieldReturnKeyType:UIReturnKeyDone];
效果如下 键盘ReturnKey变成UIReturnKeyNext 最后一个输入框ReturnKey变成UIReturnKeyDone
此功能也可以通过
self.textField.returnKeyType = UIReturnKeyNext;
self.textField1.returnKeyType = UIReturnKeyNext;
self.textField2.returnKeyType = UIReturnKeyDone;
和UITextFieldDelegate的
- (BOOL)textFieldShouldReturn:(UITextField *)textField;
或UITextViewDelegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if ([text isEqualToString:@"\n"]) {
}
}
来实现
IQTextView.h
//自定义带placeholder的textView
@interface IQTextView : UITextView
//占位符
@property(nullable, nonatomic,copy) IBInspectable NSString *placeholder;
//属性化占位符
@property(nullable, nonatomic,copy) IBInspectable NSAttributedString *attributedPlaceholder;
//占位符颜色
@property(nullable, nonatomic,copy) IBInspectable UIColor *placeholderTextColor;
@end
IQUIScrollView+Additions.h
@interface UIScrollView (Additions)
//如果为YES,则scrollview将忽略用于调整textfield位置的滚动。默认为NO。
@property(nonatomic, assign) BOOL shouldIgnoreScrollingAdjustment;
//如果为YES,则在显示键盘时,scrollview将忽略content inset。默认为NO。
@property(nonatomic, assign) BOOL shouldIgnoreContentInsetAdjustment;
//如果为YES,从scrollView退出时恢复 scrollView.ContentOffset。默认为NO
@property(nonatomic, assign) BOOL shouldRestoreScrollViewContentOffset;
@end
@interface UITableView (PreviousNextIndexPath)
//获取上一个输入框的indexPath (内部调用)
-(nullable NSIndexPath*)previousIndexPathOfIndexPath:(nonnull NSIndexPath*)indexPath;
@end
@interface UICollectionView (PreviousNextIndexPath)
//获取上一个输入框的indexPath (内部调用)
-(nullable NSIndexPath*)previousIndexPathOfIndexPath:(nonnull NSIndexPath*)indexPath;
@end