Why do my components revert to storyboard sizes in iOS8.1?

Discussion in 'Programming General' started by kmjt, Nov 10, 2014.

Why do my components revert to storyboard sizes in iOS8.1?
  1. Unread #1 - Nov 10, 2014 at 5:10 PM
  2. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Why do my components revert to storyboard sizes in iOS8.1?

    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:
    [​IMG]


    When user clicks inside of text view (or nickname text field):
    [​IMG]


    When user starts typing into the text view:
    [​IMG]


    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:
    [​IMG]


    When user clicks inside of text view (or nickname text field):
    [​IMG]


    When user starts typing into the text view:
    [​IMG]


    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?
     
  3. Unread #2 - Nov 11, 2014 at 5:30 PM
  4. SmokeHut
    Joined:
    Aug 17, 2011
    Posts:
    1,504
    Referrals:
    0
    Sythe Gold:
    112
    Discord Unique ID:
    865859811747692554
    Discord Username:
    Okesseril#7961
    Gohan has AIDS Sythe's 10th Anniversary

    SmokeHut Great men grow tired of contentedness.
    $100 USD Donor New

    Why do my components revert to storyboard sizes in iOS8.1?

    I'm yet to upgrade to iOS 8 yet, so I haven't encountered this yet.. But some of my apps use this technique of re-sizing for the keyboard.

    But where our techniques differ is that I create and render the positions, sizes programatically so I'm not sure whether you should try that and see if it helps.

    @interface SquealViewController ()

    @end

    Where are your properties?

    Hang on whilst I go on my mac to see some of my techniques

    Code:
    - (IBAction)answerTextEditBegin:(id)sender
    {
        if (editOn == YES)
        {
            editOn = NO;
            self.questionBackground.hidden = YES;
            self.questionText.hidden = YES;
            self.a.hidden = YES;
            self.b.hidden = YES;
            self.c.hidden = YES;
            self.d.hidden = YES;
            self.titleLabel.hidden = YES;
            self.infoButton.hidden = YES;
        
            const float movementDuration = 0.3f;
            
            [UIView beginAnimations: @"anim" context: nil];
            
            [UIView setAnimationBeginsFromCurrentState: YES];
            
            [UIView setAnimationDuration: movementDuration];
        
            CGRect frameRect = self.ansOne.frame;
            frameRect.size.width = 310;
            self.ansOne.frame = frameRect;
            
            CGRect frameRect1 = self.ansTwo.frame;
            frameRect1.size.width = 310;
            self.ansTwo.frame = frameRect1;
            
            CGRect frameRect2 = self.ansThree.frame;
            frameRect2.size.width = 310;
            self.ansThree.frame = frameRect2;
            
            CGRect frameRect3 = self.ansFour.frame;
            frameRect3.size.width = 310;
            self.ansFour.frame = frameRect3;
            
            self.ansOne.frame = CGRectOffset(self.ansOne.frame, -42, -110);
            self.ansTwo.frame = CGRectOffset(self.ansTwo.frame, -42, -110);
            self.ansThree.frame = CGRectOffset(self.ansThree.frame, -42, -110);
            self.ansFour.frame = CGRectOffset(self.ansFour.frame, -42, -110);
            
            [UIView commitAnimations];
        }
    }
    
    - (IBAction)answerTextEditEnd:(id)sender
    {
        if (editOn == NO)
        {
            editOn = YES;
            self.questionBackground.hidden = NO;
            self.questionText.hidden = NO;
            self.a.hidden = NO;
            self.b.hidden = NO;
            self.c.hidden = NO;
            self.d.hidden = NO;
            self.titleLabel.hidden = NO;
            self.infoButton.hidden = NO;
            
            const float movementDuration = 0.3f;
            
            [UIView beginAnimations: @"anim" context: nil];
            
            [UIView setAnimationBeginsFromCurrentState: YES];
            
            [UIView setAnimationDuration: movementDuration];
            
            CGRect frameRect = self.ansOne.frame;
            frameRect.size.width = 252;
            self.ansOne.frame = frameRect;
            
            CGRect frameRect1 = self.ansTwo.frame;
            frameRect1.size.width = 252;
            self.ansTwo.frame = frameRect1;
            
            CGRect frameRect2 = self.ansThree.frame;
            frameRect2.size.width = 252;
            self.ansThree.frame = frameRect2;
            
            CGRect frameRect3 = self.ansFour.frame;
            frameRect3.size.width = 252;
            self.ansFour.frame = frameRect3;
            
            self.ansOne.frame = CGRectOffset(self.ansOne.frame, 42, 110);
            self.ansTwo.frame = CGRectOffset(self.ansTwo.frame, 42, 110);
            self.ansThree.frame = CGRectOffset(self.ansThree.frame, 42, 110);
            self.ansFour.frame = CGRectOffset(self.ansFour.frame, 42, 110);
            
            [UIView commitAnimations];
        }
    }
    
    So I've used edit did begin and edit did end actions as opposed to nsnotifications, perhaps try it and see if you can get it to work even if it is just temporarily.

    Also, make sure you do some sort of boolean if using offsets.
     
  5. Unread #3 - Nov 11, 2014 at 10:11 PM
  6. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Why do my components revert to storyboard sizes in iOS8.1?




    Where in your code is it detecting the top of keyboard? I'll try using the text view methods tonight and see how that goes.
     
  7. Unread #4 - Nov 11, 2014 at 10:31 PM
  8. SmokeHut
    Joined:
    Aug 17, 2011
    Posts:
    1,504
    Referrals:
    0
    Sythe Gold:
    112
    Discord Unique ID:
    865859811747692554
    Discord Username:
    Okesseril#7961
    Gohan has AIDS Sythe's 10th Anniversary

    SmokeHut Great men grow tired of contentedness.
    $100 USD Donor New

    Why do my components revert to storyboard sizes in iOS8.1?

    I didn't think your question was lacking the keyboard frame as you have

    Code:
    keyboardFrame = [self.view convertRect:rawFrame fromView:nil];
    
    The snippet I gave you was just one of my first test runs in animating for usability then onward I adapted for pretty much the same method you're using.

    also,


    NSLog(@"This doesn't get displayed when I start typing in textview");

    does this run when they start typing?
     
  9. Unread #5 - Nov 11, 2014 at 11:59 PM
  10. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Why do my components revert to storyboard sizes in iOS8.1?


    No, it does not display that text when I start typing into the text view. That was my initial thought of what might be wrong (that the keyboard may be getting detected as not present).

    I checked and textViewDidEndEditing does not run when I start typing into the keyboard either. Hmm. Still stumped :(
     
  11. Unread #6 - Nov 12, 2014 at 9:38 AM
  12. SmokeHut
    Joined:
    Aug 17, 2011
    Posts:
    1,504
    Referrals:
    0
    Sythe Gold:
    112
    Discord Unique ID:
    865859811747692554
    Discord Username:
    Okesseril#7961
    Gohan has AIDS Sythe's 10th Anniversary

    SmokeHut Great men grow tired of contentedness.
    $100 USD Donor New

    Why do my components revert to storyboard sizes in iOS8.1?

    textViewDidEndEditing runs when the editing has ended, i.e keyboard closed.

    Did begin editing runs when the textfield begins edit, so resize for the keyboard on didBegin and resize without keyboard on didEnd.
     
  13. Unread #7 - Nov 12, 2014 at 10:53 PM
  14. kmjt
    Joined:
    Aug 21, 2009
    Posts:
    14,450
    Referrals:
    8
    Sythe Gold:
    449

    kmjt -.- The nocturnal life chose me -.-
    Banned

    Why do my components revert to storyboard sizes in iOS8.1?



    I'll try that tonight. Thanks for the suggestion.
     
  15. Unread #8 - Dec 12, 2014 at 1:17 PM
  16. VirusRS
    Joined:
    Dec 9, 2014
    Posts:
    73
    Referrals:
    0
    Sythe Gold:
    0

    VirusRS Member
    Banned

    Why do my components revert to storyboard sizes in iOS8.1?

    maybe something to do with resize keyboard? I'll look after.
     
< RuneScape Classic Bot Coding, | Thread Synchronization >

Users viewing this thread
1 guest


 
 
Adblock breaks this site