Albert James Teddy
Albert James Teddy

Reputation: 1375

Change in metrics for the new iOS simulator in Xcode 6

My app runs fine in Xcode 5 on a iPad retina simulator, but when I run the simulator on the same project with Xcode 6 on the iPad retina simulator, this little bit of code :

UIView *firstResp = [self.view findFirstResponder];
CGRect firstResponderFrame = [firstResp convertRect:firstResp.bounds toView:self.view];


NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

if (firstResponderFrame.origin.y+firstResponderFrame.size.height+40-self.scrollView.contentOffset.y > self.view.window.frame.size.width-kbSize.width) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.scrollView setContentOffset:CGPointMake(0, firstResponderFrame.origin.y-self.view.window.frame.size.width/2+100+firstResponderFrame.size.height/2) animated:YES];
    });


}

Basically this code is executed when the keyboard shows up, to center the view. It now behaves very poorly; some of the time no keyboard even shows up.

No errors are showing in Xcode 6. The metric seems to be the same from my initial test of different frames. I'm using NSNotification for the keyboard: maybe that's the cause?

This code is also in a view controller inside a container view controller in another VC.

It's not really a specific question, but I'm very curious as why it will not work. I'm running on iOS 7.1 in both versions of Xcode. The simulator in Xcode 6 shows iOS 8 on the window's title bar, but my app is configured to run on iOS 7.1.

**

edit:

** NVM.. found it. I didn't test thoroughly enough before asking. For those interested, there was a bug in IOS 7 that cause the height and width to remain the same even though the orientation had change, it happened to my knowledge to the view.window.frame and the keyboard size .. This bug seem to be gone for iOS 8.

Upvotes: 3

Views: 1098

Answers (1)

Albert James Teddy
Albert James Teddy

Reputation: 1375

A work-around the iOS 7 bug for width and height in portrait orientation - i hope this will help somebody in the future for iOS8 migration.

// Called when the UIKeyboardDidShowNotification is sent.
   - (void)keyboardWillShow:(NSNotification*)aNotification
   {
   [self UpdatePatientFromForm];
    UIView *firstResp = [self.view findFirstResponder];
    CGRect firstResponderFrame = [firstResp convertRect:firstResp.bounds toView:self.view];


    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    //Account for iOS 7 inversion of height and width
    CGFloat kbHeight = (kbSize.width>kbSize.height)? kbSize.height : kbSize.width;
    CGFloat windowHeight = (self.view.window.frame.size.width>self.view.window.frame.size.height)? self.view.window.frame.size.height : self.view.window.frame.size.width;
    CGFloat visibleScreenHeight = windowHeight - kbHeight;


    if (firstResponderFrame.origin.y+firstResponderFrame.size.height+100-self.scrollView.contentOffset.y > visibleScreenHeight) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.scrollView setContentOffset:CGPointMake(0, firstResponderFrame.origin.y-windowHeight/2+100+firstResponderFrame.size.height/2) animated:YES];
        });   
    }  
}

Upvotes: 2

Related Questions