测试已测试 | ✗ |
语言语言 | Obj-CObjective C |
许可 | MIT |
发布最后发布 | 2017年6月 |
由以下人员维护: Mikkel Selsoe Sorensen,Soren Ulrikkeholm,Philip Bruce,Peter Gammelgaard Poulsen,Jonas Lysgaard-Hansen,Marcus Mattsson,Jakob Vinther-Larsen。
SHPKeyboardAwareness 可以让您知道何时出现键盘,并准确地告诉您有关第一响应者被遮挡程度、哪个视图是第一响应者、键盘动画曲线、持续时间等多方面的信息。
SHPKeyboardAwareness 不会劫持您任何视图的代理方法,它既不派生也不会要求您派生任何内容。SHPKeyboardAwareness 不会对您的代码进行任何妥协。
启用 SHPKeyboardAwareness
的同时,每当键盘出现、消失或第一响应者发生变化时,您都会收到通知。 SHPKeyboardAwareness
跟踪第一响应者,并在通知中告诉您视图是否被键盘遮挡。如果需要,您也可以指定要跟踪的特定视图,而不是当前的第一响应者。
如果冲突的视图是 UITextView,将计算所需的偏移量用于光标(插入点)
请参阅 Example/
文件夹中包含的示例项目。
对于不需要自行处理的默认实现,使用
[SHPKeyboardAwarenessObserver ObserveView:view verticalConstraint:bottomConstraint conflictingViewPadding:20];
[SHPKeyboardAwarenessObserver ObserveScrollView:scrollView conflictingViewPadding:20];
如果需要更专业的实现,您可以使用代理方法。
SHPKeyboardAwareness
继承自 NSObject
,易于使用。要设置 SHPKeyboardAwareness
,只需调用
[SHPKeyboardAwarenessObserver ObserveWithDeledgate:self];
这将订阅 self
的任何键盘事件。
Note that when self deallocates it is automatically unsubscribed from further keyboard events.
第一响应者(UITextField
或 UITextView
)将作为通知的一部分传入。要接收通知,请遵守协议 SHPKeyboardAwarenessClient
并实现所需的方法
- (void)keyboardTriggeredEvent:(SHPKeyboardEvent *)keyboardEvent
您通过代理方法收到的 SHPKeyboardEvent
包含对任何对立视图的引用,该引用位于属性中
@property (nonatomic, readonly) UIView *conflictingView;
视图被键盘遮挡的程度在属性中指定
@property (nonatomic, readonly) CGFloat requiredViewOffset;
这些信息可以用来自动移动视图,以便它不再被遮挡
在这个示例中,我们将视图控制器订阅到可能与应用程序页脚冲突的任何第一响应者 UITextField
或 UITextView
的通知
@import SHPKeyboardAwareness;
@interface ViewController ()
@property (nonatomic, strong) SHPKeyboardAwarenessObserver *keyboardAwareness;
@end
@implementation ViewController
...
- (void)viewDidLoad {
[super viewDidLoad];
// Subscribe to keyboard events. The receiver (self) will be automatically unsubscribed when deallocated
self.keyboardAwareness = [SHPKeyboardAwarenessObserver ObserveWithDelegate:self];
}
- (void)keyboardTriggeredEvent:(SHPKeyboardEvent *)keyboardEvent {
CGFloat offset = 0;
switch (keyboardEvent.keyboardEventType) {
case SHPKeyboardEventTypeShow:
// Keyboard will appear. Calculate the new offset from the provided offset
offset = collectionView.contentOffset.y - keyboardEvent.requiredViewOffset;
// Save the current view offset into the event to retrieve it later
keyboardEvent.originalOffset = collectionView.contentOffset.y;
break;
case SHPKeyboardEventTypeHide:
// Keyboard will hide. Reset view offset to its state before keyboard appeared
offset = keyboardEvent.originalOffset;
break;
default:
break;
}
// Animate the offset change with the provided curve and duration
[UIView animateWithDuration:keyboardEvent.keyboardAnimationDuration
delay:0
options:keyboardEvent.keyboardAnimationOptionCurve
animations:^{
self.collectionView.contentOffset = CGPointMake(collectionView.contentOffset.x, offset);
self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, keyboardEvent.keyboardFrame.size.height, 0);
self.collectionView.scrollIndicatorInsets = self.collectionView.contentInset;
} completion:nil];
}
...
@end
上面的示例中的 SHPKeyboardEvent
会告诉你
UITextField
或 UITextView
实例)frame
和可见屏幕 frame
此外
在这个示例中,我们订阅到当特定视图被键盘遮挡时的通知。例如,您可能有一个容器视图围绕 UITextField
或 UITextView
,您希望保持清晰,例如,因为容器视图包含一个 UIButton
,它必须完全可见。
@import SHPKeyboardAwareness;
@interface ViewController ()
@property (nonatomic, strong) SHPKeyboardAwarenessObserver *keyboardAwareness;
@end
@implementation ViewController
...
- (void)viewDidLoad {
[super viewDidLoad];
// Subscribe to keyboard events. The receiver (self) will be automatically unsubscribed when deallocated
self.keyboardAwareness = [SHPKeyboardAwarenessObserver ObserveView: _myView withDelegate:self];
}
- (void)keyboardTriggeredEvent:(SHPKeyboardEvent *)event {
// This event fires when and only when any part of the provided view (containerView) conflicts
// with the keyboard that is about to appear. Events for other textFields etc are ignored.
// For the sake of simplicity, we assume the containerFrame's origin.y is 0 when keyboard is not visible
// But you can use the originalOffset property as shown above to preserve its origin.
CGRect containerFrame = containerView.frame;
containerFrame.origin.y += event.requiredViewOffset;
// Animate the offset change with the provided curve and duration
[UIView animateWithDuration:event.keyboardAnimationDuration
delay:0
options:event.keyboardAnimationOptionCurve
animations:^{
containerView.frame = containerFrame;
} completion:nil];
}
...
@end
如果您发现错误或对功能有建议,请创建拉取请求或打开问题,指定您的问题/功能请求。
我们希望您喜欢使用 SHPKeyboardAwareness
并且它能帮助您避免在应用程序中的令人讨厌的键盘遮挡。