Last active
August 29, 2015 14:14
-
-
Save vijaysharm/b339f809636e87c34666 to your computer and use it in GitHub Desktop.
Facebook Paper Login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "PaperLoginViewController.h" | |
@interface PaperLoginViewController() <UITextFieldDelegate> | |
@property (strong, nonatomic) UILabel *titleLabel; | |
@property (strong, nonatomic) UITextField *usernameField; | |
@property (strong, nonatomic) UITextField *passwordField; | |
@property (strong, nonatomic) UIView *separator; | |
@property (strong, nonatomic) NSMutableArray *keyboardShownConstraints; | |
@property (strong, nonatomic) NSMutableArray *keyboardHiddenConstraints; | |
@end | |
@implementation PaperLoginViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
UIColor *transparentWhite = [UIColor colorWithWhite:1.0 alpha:0.5]; | |
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
_titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
_titleLabel.text = @"Login"; | |
_titleLabel.font = [UIFont fontWithName:@"AvenirNext-UltraLight" size:60.0f]; | |
_titleLabel.textColor = [UIColor whiteColor]; | |
[_titleLabel sizeToFit]; | |
[self.view addSubview:_titleLabel]; | |
_usernameField = [[UITextField alloc] initWithFrame:CGRectZero]; | |
_usernameField.delegate = self; | |
_usernameField.translatesAutoresizingMaskIntoConstraints = NO; | |
_usernameField.textColor = [UIColor whiteColor]; | |
_usernameField.tintColor = [UIColor whiteColor]; | |
_usernameField.font = [UIFont fontWithName:@"AvenirNext-UltraLight" size:30.0f]; | |
_usernameField.enablesReturnKeyAutomatically = YES; | |
_usernameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Username" | |
attributes:@{NSForegroundColorAttributeName: transparentWhite}]; | |
[_usernameField setKeyboardType:UIKeyboardTypeEmailAddress]; | |
[_usernameField setReturnKeyType:UIReturnKeyNext]; | |
[self.view addSubview:_usernameField]; | |
_passwordField = [[UITextField alloc] initWithFrame:CGRectZero]; | |
_passwordField.delegate = self; | |
_passwordField.translatesAutoresizingMaskIntoConstraints = NO; | |
_passwordField.textColor = [UIColor whiteColor]; | |
_passwordField.tintColor = [UIColor whiteColor]; | |
_passwordField.font = [UIFont fontWithName:@"AvenirNext-UltraLight" size:30.0f]; | |
_passwordField.secureTextEntry = YES; | |
_passwordField.enablesReturnKeyAutomatically = YES; | |
_passwordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Password" | |
attributes:@{NSForegroundColorAttributeName: transparentWhite}]; | |
[_passwordField setKeyboardType:UIKeyboardTypeDefault]; | |
[_passwordField setReturnKeyType:UIReturnKeyGo]; | |
[self.view addSubview:_passwordField]; | |
_separator = [[UIView alloc] initWithFrame:CGRectZero]; | |
_separator.translatesAutoresizingMaskIntoConstraints = NO; | |
_separator.backgroundColor = transparentWhite; | |
[self.view addSubview:_separator]; | |
NSDictionary *views = @{ | |
@"title": _titleLabel, | |
@"username": _usernameField, | |
@"password": _passwordField, | |
@"separator": _separator | |
}; | |
NSLayoutConstraint *titleCenter = [NSLayoutConstraint constraintWithItem:_titleLabel | |
attribute:NSLayoutAttributeCenterX | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.view | |
attribute:NSLayoutAttributeCenterX | |
multiplier:1.0 constant:0.0f]; | |
NSArray *verticalConstraints = [NSLayoutConstraint | |
constraintsWithVisualFormat:@"V:|-top-[title]-space-[username]-(8)-[separator(1)]-(8)-[password]" | |
options:kNilOptions | |
metrics:@{ | |
@"top": @(120), | |
@"space": @(100) | |
} | |
views:views]; | |
NSArray *usernameHorizontalConstraints = [NSLayoutConstraint | |
constraintsWithVisualFormat:@"H:|-[username]-|" | |
options:kNilOptions | |
metrics:nil | |
views:views]; | |
NSArray *separatorHorizontalConstraints = [NSLayoutConstraint | |
constraintsWithVisualFormat:@"H:|-[separator]-|" | |
options:kNilOptions | |
metrics:nil | |
views:views]; | |
NSArray *passwordHorizontalConstraints = [NSLayoutConstraint | |
constraintsWithVisualFormat:@"H:|-[password]-|" | |
options:kNilOptions | |
metrics:nil | |
views:views]; | |
_keyboardHiddenConstraints = [NSMutableArray array]; | |
[_keyboardHiddenConstraints addObject:titleCenter]; | |
[_keyboardHiddenConstraints addObjectsFromArray:verticalConstraints]; | |
[_keyboardHiddenConstraints addObjectsFromArray:usernameHorizontalConstraints]; | |
[_keyboardHiddenConstraints addObjectsFromArray:separatorHorizontalConstraints]; | |
[_keyboardHiddenConstraints addObjectsFromArray:passwordHorizontalConstraints]; | |
verticalConstraints = [NSLayoutConstraint | |
constraintsWithVisualFormat:@"V:|-top-[title]-space-[username]-(8)-[separator(1)]-(8)-[password]" | |
options:kNilOptions | |
metrics:@{ | |
@"top": @(70), | |
@"space": @(40) | |
} | |
views:views]; | |
_keyboardShownConstraints = [NSMutableArray array]; | |
[_keyboardShownConstraints addObject:titleCenter]; | |
[_keyboardShownConstraints addObjectsFromArray:verticalConstraints]; | |
[_keyboardShownConstraints addObjectsFromArray:usernameHorizontalConstraints]; | |
[_keyboardShownConstraints addObjectsFromArray:separatorHorizontalConstraints]; | |
[_keyboardShownConstraints addObjectsFromArray:passwordHorizontalConstraints]; | |
[self.view addConstraints:_keyboardHiddenConstraints]; | |
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] | |
initWithTarget:self action:@selector(respondToTapGesture:)]; | |
tapRecognizer.numberOfTapsRequired = 1; | |
[self.view addGestureRecognizer:tapRecognizer]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; | |
} | |
- (BOOL)prefersStatusBarHidden { | |
return YES; | |
} | |
#pragma mark - UITextFieldDelegate | |
- (BOOL)textFieldShouldReturn:(UITextField *)textField { | |
if (textField == _usernameField) { | |
[_passwordField becomeFirstResponder]; | |
} else if (textField == _passwordField) { | |
[_passwordField resignFirstResponder]; | |
// TODO: Should go ahead with login | |
} | |
return YES; | |
} | |
#pragma mark - Tap Gesture callback | |
- (void)respondToTapGesture:(id)obj { | |
[_usernameField resignFirstResponder]; | |
[_passwordField resignFirstResponder]; | |
} | |
#pragma mark - Keyboard callbacks | |
- (void)keyboardWillShow:(NSNotification *)notification { | |
[self.view removeConstraints:_keyboardHiddenConstraints]; | |
[self.view addConstraints:_keyboardShownConstraints]; | |
[UIView animateWithDuration:0.3f animations:^{ | |
[self.view layoutIfNeeded]; | |
}]; | |
} | |
- (void)keyboardWillHide:(NSNotification *)notification { | |
[self.view removeConstraints:_keyboardShownConstraints]; | |
[self.view addConstraints:_keyboardHiddenConstraints]; | |
[UIView animateWithDuration:0.3f animations:^{ | |
[self.view layoutIfNeeded]; | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment