https://github.com/jpsim/jpskeyboardlayoutguide
Easily make your Auto Layout view controllers keyboard aware
https://github.com/jpsim/jpskeyboardlayoutguide
Last synced: 12 months ago
JSON representation
Easily make your Auto Layout view controllers keyboard aware
- Host: GitHub
- URL: https://github.com/jpsim/jpskeyboardlayoutguide
- Owner: jpsim
- License: mit
- Created: 2014-03-26T18:28:40.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2017-03-07T15:42:17.000Z (over 9 years ago)
- Last Synced: 2025-06-22T04:46:59.154Z (about 1 year ago)
- Language: Objective-C
- Homepage: jpsim.com/keyboard-layout-guide
- Size: 20.5 KB
- Stars: 69
- Watchers: 7
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# JPSKeyboardLayoutGuide
`JPSKeyboardLayoutGuide` lets you easily make your autolayout view controllers keyboard aware. Think of it as `bottomLayoutGuide`, if it moved along with the keyboard frame.
This makes it dead simple to vertically center items in a view and have them stay centered when the keyboard appears/disappears.
## Installation
### From CocoaPods
Add `pod 'JPSKeyboardLayoutGuide'` to your Podfile.
### Manually
Drag the `JPSKeyboardLayoutGuide` folder into your project.
## Usage
`JPSKeyboardLayoutGuide` is a category of `UIViewController`, so for any controller where you want to adopt the behaviour previously described, you must override and call these methods in it's respective callbacks:
```objective-c
- (void)jps_viewDidLoad;
- (void)jps_viewWillAppear:(BOOL)animated;
- (void)jps_viewDidDisappear:(BOOL)animated;
```
A sample implementation would be like this:
```objective-c
#import "JPSKeyboardLayoutGuideViewController.h"
@interface LoginVC : UIViewController
@end
@implementation LoginVC
- (void)viewDidLoad {
[super viewDidLoad];
[self jps_viewDidLoad];
[self setupLoginField];
}
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self jps_viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self jps_viewDidDisappear:animated];
}
- (void)setupLoginField {
self.loginField = [[UITextField alloc] init];
self.loginField.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.loginField];
self.loginField.placeholder = @"username";
// Constraints
NSLayoutConstraint *centerX = [NSLayoutConstraint constraintWithItem:self.loginField
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f];
NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.loginField
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.keyboardLayoutGuide
attribute:NSLayoutAttributeTop
multiplier:1.0f
constant:-10.0f];
[self.view addConstraints:@[centerX, bottom]];
}
@end
```
When importing `JPSKeyboardLayoutGuide`, you'll see `keyboardLayoutGuide` in addition to `topLayoutGuide` and `bottomLayoutGuide`. All these guides behave in the same way.
For more details on layout guides, refer to Apple's documentation on the [`UILayoutSupport` Protocol](https://developer.apple.com/library/ios/documentation/uikit/reference/UILayoutSupport_Protocol/Reference/Reference.html)
## License
MIT Licensed.