-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsEditGoalsViewController.m
More file actions
executable file
·206 lines (171 loc) · 8.31 KB
/
SettingsEditGoalsViewController.m
File metadata and controls
executable file
·206 lines (171 loc) · 8.31 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//
// SettingsEditGoalsViewController.m
// Junction
//
// Created by Bobby Ren on 4/7/13.
//
//
#import "SettingsEditGoalsViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDelegate.h"
static AppDelegate * appDelegate;
@interface SettingsEditGoalsViewController ()
@end
@implementation SettingsEditGoalsViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
UIImage * headerbg = [UIImage imageNamed:@"header_bg"];
[self.navigationController.navigationBar setBackgroundImage:headerbg forBarMetrics:UIBarMetricsDefault];
UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[titleView setFont:[UIFont boldSystemFontOfSize:23]];
[titleView setTextColor:[UIColor whiteColor]];
[titleView setBackgroundColor:[UIColor colorWithRed:14.0/255.0 green:158.0/255.0 blue:205.0/255.0 alpha:1]];
[titleView setTextAlignment:NSTextAlignmentCenter];
titleView.text = @"Goals";
UIFont * font = titleView.font;
CGRect frame = CGRectMake(0, 0, [self.navigationItem.title sizeWithFont:font].width, 44);
frame.origin.x = 320 - frame.size.width / 2;
[titleView setFrame:frame];
self.navigationItem.titleView = titleView;
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"icon-back"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(10, 0, 30, 30)];
UIBarButtonItem * backbutton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:backbutton];
self.view.backgroundColor = COLOR_FAINTBLUE;
UIToolbar* keyboardDoneButtonView1 = [[UIToolbar alloc] init];
keyboardDoneButtonView1.barStyle = UIBarStyleBlack;
keyboardDoneButtonView1.translucent = YES;
keyboardDoneButtonView1.tintColor = nil;
[keyboardDoneButtonView1 sizeToFit];
UIBarButtonItem* doneButton1 = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"Done")
style:UIBarButtonItemStyleBordered target:self
action:@selector(doneEditing:)];
[keyboardDoneButtonView1 setItems:[NSArray arrayWithObjects:doneButton1, nil]];
textViewLookingFor.inputAccessoryView = keyboardDoneButtonView1;
textViewTalkAbout.inputAccessoryView = keyboardDoneButtonView1;
[textViewLookingFor.layer setCornerRadius:5];
[textViewTalkAbout.layer setCornerRadius:5];
[textViewLookingFor.layer setBorderColor:[COLOR_LIGHTBLUE CGColor]];
[textViewTalkAbout.layer setBorderColor:[COLOR_LIGHTBLUE CGColor]];
[textViewLookingFor.layer setBorderWidth:1];
[textViewTalkAbout.layer setBorderWidth:1];
[labelLookingFor setFont:[UIFont fontWithName:@"SansusWebissimo" size:12]];
[labelTalkAbout setFont:[UIFont fontWithName:@"SansusWebissimo" size:12]];
[labelLookingFor setTextColor:COLOR_LIGHTBLUE];
[labelTalkAbout setTextColor:COLOR_LIGHTBLUE];
UserInfo * myUserInfo = appDelegate.myUserInfo;
textViewLookingFor.text = myUserInfo.lookingFor;
textViewTalkAbout.text = myUserInfo.talkAbout;
// keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc {
//keyboard notifications
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark UITextViewDelegate
-(void)textViewDidEndEditing:(UITextView *)textView {
[textView resignFirstResponder];
currentTextView = nil;
}
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {
currentTextView = textView;
return YES;
}
-(void)doneEditing:(id)sender {
// don't save here
[textViewLookingFor resignFirstResponder];
[textViewTalkAbout resignFirstResponder];
}
-(void)goBack:(id)sender {
// save
UserInfo * myUserInfo = appDelegate.myUserInfo;
myUserInfo.talkAbout = textViewTalkAbout.text;
myUserInfo.lookingFor = textViewLookingFor.text;
[[myUserInfo toPFObject] saveEventually:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(@"Personal information Updated!");
[[NSNotificationCenter defaultCenter] postNotificationName:kMyUserInfoDidChangeNotification object:nil];
}
else {
NSLog(@"Saving personal information error: %@", error);
}
}];
// dismiss
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark scrollview and keyboard
- (void)keyboardWillShow:(NSNotification *)n
{
// This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown.
if (keyboardIsShown) {
[scrollView setFrame:self.view.frame];
// return;
}
NSDictionary* userInfo = [n userInfo];
// get the sizshouldbegine of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the noteView
CGRect viewFrame = scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height -= (keyboardSize.height);// - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:0.3];
[scrollView setFrame:viewFrame];
CGPoint contentOffset;
if (currentTextView == textViewLookingFor)
contentOffset = CGPointMake(0, labelLookingFor.frame.origin.y);
else if (currentTextView == textViewTalkAbout)
contentOffset = CGPointMake(0, labelTalkAbout.frame.origin.y);
else
contentOffset = scrollView.contentOffset;
[scrollView setContentOffset:contentOffset];
[UIView commitAnimations];
keyboardIsShown = YES;
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
// get the size of the keyboard
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// resize the scrollview
CGRect viewFrame = scrollView.frame;
// I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.
viewFrame.size.height += (keyboardSize.height);// - kTabBarHeight);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
// The kKeyboardAnimationDuration I am using is 0.3
[UIView setAnimationDuration:0.3];
[scrollView setFrame:viewFrame];
[scrollView setContentOffset:CGPointMake(0, 0)];
[UIView commitAnimations];
keyboardIsShown = NO;
}
-(void)hideKeyboard {
[textViewLookingFor resignFirstResponder];
[textViewTalkAbout resignFirstResponder];
}
@end