I have a view controller where when the user clicks in the text view, it will expand the text view and other components all the way to the top of the keyboard. It works as intended for iOS7:
As soon as user enters view:
When user clicks inside of text view (or nickname text field):
When user starts typing into the text view:
So that is on the iOS7 simulator in xcode6. This is the proper functionality. The following is iOS8.1 on xcode6 simulator which is giving me grief:
As soon as user enters view:
When user clicks inside of text view (or nickname text field):
When user starts typing into the text view:
For some reason as soon as I start typing on the keyboard, the textview and other components like nickname text field, character count label, post button, border lines etc. all revert back in size to what I set them on the storyboard (it doesn't do this on iOS7). Here is my code for the view controller:
I am stumped. Why does it keep reverting back to its initial storyboard frame when I start typing in it?
As soon as user enters view:
When user clicks inside of text view (or nickname text field):
When user starts typing into the text view:
So that is on the iOS7 simulator in xcode6. This is the proper functionality. The following is iOS8.1 on xcode6 simulator which is giving me grief:
As soon as user enters view:
When user clicks inside of text view (or nickname text field):
When user starts typing into the text view:
For some reason as soon as I start typing on the keyboard, the textview and other components like nickname text field, character count label, post button, border lines etc. all revert back in size to what I set them on the storyboard (it doesn't do this on iOS7). Here is my code for the view controller:
Code:
#import "SquealViewController.h"
#import "UniqueUserIdentification.h"
#import "DailyViewController.h"
#import "Score.h"
@interface SquealViewController ()
@end
@implementation SquealViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[UIView setAnimationsEnabled:NO];
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
if([[UIDevice currentDevice] isMultitaskingSupported])
{
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
lastTableViewRowRecent = 0;
lastTableViewRowPopular = 0;
[self.view setBackgroundColor:[UIColor colorWithRed:(255/255.0) green:(221/255.0) blue:(85/255.0) alpha:1.0f]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
tap.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tap];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:self.view.window];
self.textView.keyboardType = UIKeyboardTypeASCIICapable;
self.nicknameTextField.keyboardType = UIKeyboardTypeASCIICapable;
self.textView.autocorrectionType = UITextAutocorrectionTypeNo;
self.nicknameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
self.textView.editable = false;
self.nicknameTextField.enabled = false;
self.postButton.enabled = false;
activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
activityIndicator.layer.cornerRadius = 05;
activityIndicator.opaque = NO;
activityIndicator.center = self.view.center;
[activityIndicator setColor:[UIColor colorWithRed:255/255.0 green:221/255.0 blue:85/225.0 alpha:1.0]];
[self.view addSubview: activityIndicator];
CGAffineTransform transform = CGAffineTransformMakeScale(3.5f, 3.5f);
activityIndicator.transform = transform;
[activityIndicator startAnimating];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.org/getScore.php?userID=%@", [UniqueUserIdentification getUserID]]];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
scoreArray = [[NSMutableArray alloc] init];
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
dispatch_async(dispatch_get_main_queue(), ^{
if(jsonArray.count > 0)
{
NSString *upVotes = [[jsonArray objectAtIndex:0] objectForKey:@"upvotes"];
NSString *downVotes = [[jsonArray objectAtIndex:0] objectForKey:@"downvotes"];
int score = [upVotes integerValue]-[downVotes integerValue];
self.scoreLabel.text = [NSString stringWithFormat:@"%i", score];
}
else
{
self.scoreLabel.text = @"N/A";
}
self.textView.editable = true;
self.nicknameTextField.enabled = true;
self.postButton.enabled = true;
[activityIndicator stopAnimating];
activityIndicator.hidden = true;
});
});
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)textViewDidChange:(UITextView *)textView
{
int length = textView.text.length;
if(length > -999)
self.countLabel.text=[NSString stringWithFormat:@"%i", 300-length];
if([self.countLabel.text integerValue] <= 0)
self.countLabel.textColor = [UIColor redColor];
else
self.countLabel.textColor = [UIColor blackColor];
if(length > 0)
self.textViewPlaceholderLabel.hidden = true;
else
self.textViewPlaceholderLabel.hidden = false;
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"])
[textView resignFirstResponder];
if([self.textView.text length] > 299)
self.textView.text = [self.textView.text substringToIndex:299];
return true;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:@"\n"])
[textField resignFirstResponder];
if([self.nicknameTextField.text length] > 13)
self.nicknameTextField.text = [self.nicknameTextField.text substringToIndex:13];
return true;
}
- (IBAction)postButtonPressed:(id)sender
{
if([self.textView.text length] > 300)
{
return;
}
NSString *encodedSquealString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)self.textView.text,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
NSString *nickname = self.nicknameTextField.text;
if([nickname length] == 0)
nickname = @"Anonymous";
NSString *encodedNicknameString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)nickname,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
{
if([[UIDevice currentDevice] isMultitaskingSupported])
{
UIApplication *application = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
self.textView.editable = false;
self.nicknameTextField.enabled = false;
self.postButton.enabled = false;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *strURL = [NSString stringWithFormat:@"http://example.org/post.php?squealID=0&postType=0&nickname=%@&message=%@&byUserID=%@", encodedNicknameString, encodedSquealString, [UniqueUserIdentification getUserID]];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
DailyViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"DailyView"];
[self presentViewController:vc animated:NO completion:nil];
});
});
}
}
}
- (void)keyboardDidShow:(NSNotification *)notif
{
[UIView setAnimationsEnabled:NO];
self.scoreLabel.hidden = true;
NSDictionary *info = notif.userInfo;
NSValue *value = info[UIKeyboardFrameEndUserInfoKey];
CGRect rawFrame = [value CGRectValue];
keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
[self.borderLineTen setFrame:CGRectMake(0, keyboardFrame.origin.y-2, 320, 2)];
[self.thickBorderLineThree setFrame:CGRectMake(0, keyboardFrame.origin.y-10, 320, 8)];
[self.borderLineSeven setFrame:CGRectMake(0, keyboardFrame.origin.y-12, 192, 2)];
[self.borderLineEight setFrame:CGRectMake(200, keyboardFrame.origin.y-12, 56, 2)];
[self.borderLineNine setFrame:CGRectMake(264, keyboardFrame.origin.y-12, 56, 2)];
[self.nicknameTextField setFrame:CGRectMake(10, keyboardFrame.origin.y-50, 182, 38)];
[self.leftNicknameLabel setFrame:CGRectMake(0, keyboardFrame.origin.y-50, 10, 38)];
[self.borderLineFourteen setFrame:CGRectMake(190, keyboardFrame.origin.y-50, 2, 38)];
[self.borderLineEleven setFrame:CGRectMake(200, keyboardFrame.origin.y-50, 2, 38)];
[self.borderLineTwelve setFrame:CGRectMake(254, keyboardFrame.origin.y-50, 2, 38)];
[self.borderLineThirteen setFrame:CGRectMake(264, keyboardFrame.origin.y-50, 2, 38)];
[self.thickBorderLineFour setFrame:CGRectMake(192, keyboardFrame.origin.y-50, 8, 40)];
[self.thickBorderLineFive setFrame:CGRectMake(256, keyboardFrame.origin.y-50, 8, 40)];
[self.countLabel setFrame:CGRectMake(200, keyboardFrame.origin.y-50, 56, 38)];
[self.postButton setFrame:CGRectMake(264, keyboardFrame.origin.y-50, 56, 38)];
[self.borderLineFour setFrame:CGRectMake(0, keyboardFrame.origin.y-50, 192, 2)];
[self.borderLineFive setFrame:CGRectMake(200, keyboardFrame.origin.y-50, 56, 2)];
[self.borderLineSix setFrame:CGRectMake(264, keyboardFrame.origin.y-50, 56, 2)];
[self.thickBorderLineTwo setFrame:CGRectMake(0, keyboardFrame.origin.y-58, 320, 8)];
[self.borderLineThree setFrame:CGRectMake(0, keyboardFrame.origin.y-60, 320, 2)];
[self.textView setFrame:CGRectMake(0, 86, 320, keyboardFrame.origin.y-86-60)];
}
- (void)keyboardWillHide:(NSNotification *)notif
{
[UIView setAnimationsEnabled:NO];
NSLog(@"This doesn't get displayed when I start typing in textview");
/*
[self.borderLineTen setFrame:CGRectMake(0, 262, 320, 2)];
[self.thickBorderLineThree setFrame:CGRectMake(0, 254, 320, 8)];
[self.borderLineSeven setFrame:CGRectMake(0, 252, 192, 2)];
[self.borderLineEight setFrame:CGRectMake(200, 252, 56, 2)];
[self.borderLineNine setFrame:CGRectMake(264, 252, 56, 2)];
[self.nicknameTextField setFrame:CGRectMake(10, 214, 182, 38)];
[self.leftNicknameLabel setFrame:CGRectMake(0, 214, 10, 38)];
[self.borderLineFourteen setFrame:CGRectMake(190, 216, 2, 38)];
[self.borderLineEleven setFrame:CGRectMake(200, 216, 2, 38)];
[self.borderLineTwelve setFrame:CGRectMake(254, 216, 2, 38)];
[self.borderLineThirteen setFrame:CGRectMake(264, 216, 2, 38)];
[self.thickBorderLineFour setFrame:CGRectMake(192, 214, 8, 40)];
[self.thickBorderLineFive setFrame:CGRectMake(256, 214, 8, 40)];
[self.countLabel setFrame:CGRectMake(200, 214, 56, 38)];
[self.postButton setFrame:CGRectMake(264, 214, 56, 38)];
[self.borderLineFour setFrame:CGRectMake(0, 214, 192, 2)];
[self.borderLineFive setFrame:CGRectMake(200, 214, 56, 2)];
[self.borderLineSix setFrame:CGRectMake(264, 214, 56, 2)];
[self.thickBorderLineTwo setFrame:CGRectMake(0, 206, 320, 8)];
[self.borderLineThree setFrame:CGRectMake(0, 204, 320, 2)];
[self.textView setFrame:CGRectMake(0, 86, 320, 118)];
*/
self.scoreLabel.hidden = false;
}
-(void)dismissKeyboard
{
[self.textView resignFirstResponder];
[self.nicknameTextField resignFirstResponder];
}
@end
I am stumped. Why does it keep reverting back to its initial storyboard frame when I start typing in it?



