summaryrefslogtreecommitdiff
path: root/Juick/ViewControllers/NewPostViewController.m
blob: 9643b7849fb5dd5384c9e0f9bc700c6e9f48d657 (plain) (blame)
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
//
//  NewPostViewController.m
//  Juick
//
//  Created by Vitaly Takmazov on 07.11.13.
//  Copyright (c) 2013 com.juick. All rights reserved.
//

#import "NewPostViewController.h"
#import "ColorScheme.h"

@interface NewPostViewController ()

@property (nonatomic, assign) BOOL didSetupConstraints;
@property (nonatomic, assign) int paddingValue;

@end

@implementation NewPostViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.visibleViewController.title = @"Post";
	self.navigationController.visibleViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                                          target:self action:@selector(cancelCompose)];
    self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
                                                                                           target:self action:@selector(doneCompose)];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [self.textView becomeFirstResponder];
}

- (void) cancelCompose {
    [self.textView resignFirstResponder];
    [self.navigationController.visibleViewController.navigationController popViewControllerAnimated:YES];
}

- (void) doneCompose {
    [self.navigationController.visibleViewController.navigationController popToRootViewControllerAnimated:YES];
}

-(void) keyboardDidShow:(NSNotification *)sender {
    CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect newFrame = [self.view convertRect:frame fromView:[[UIApplication sharedApplication] delegate].window];    
    [self.view layoutIfNeeded];
    [UIView animateWithDuration:[sender.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] animations:^{
        self.bottomConstraint.constant = newFrame.origin.y - CGRectGetHeight(self.view.frame) + self.paddingValue;
        [self.view layoutIfNeeded];
    }];
}
-(void) keyboardWillHide:(NSNotification *)sender {
    self.bottomConstraint.constant = self.paddingValue;
    [self.view layoutIfNeeded];
}
@end