質問

I have a DetailViewController with a bottom toolbar.

This toolbar has UIButtons and also UITextField objects.

In order to have these UITextFields visible whilst editing, I use the following routines:

- (void) returnMainViewToInitialposition:(NSNotification*)aNotification{
    [self scrollViewForKeyboard:aNotification up:NO];
}

- (void) scrollViewForKeyboard:(NSNotification*)aNotification up:(BOOL) up{
    if (_detailItem && ( self.addressTextField.editing || self.urlTextField.editing || self.titleTextField.editing )) {
        NSDictionary* userInfo = [aNotification userInfo];

        // Get animation info from userInfo
        NSTimeInterval animationDuration;
        UIViewAnimationCurve animationCurve;
        CGRect keyboardFrame;
        [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
        [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
        [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
        CGFloat _keyboardHeight = ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) ? keyboardFrame.size.height : keyboardFrame.size.width;
        // Animate up or down
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:animationDuration];
        [UIView setAnimationCurve:animationCurve];
        [self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + _keyboardHeight * (up?-1:1), self.view.frame.size.width, self.view.frame.size.height)];
        [UIView commitAnimations];
        NSLog(@"detailView bounds: %.0f, (%.0f + %.0f x %d), %3.0f, %3.0f", self.view.frame.origin.x, self.view.frame.origin.y + _keyboardHeight * (up?-1:1), _keyboardHeight, (up?-1:1), self.view.frame.size.width, self.view.frame.size.height);

    }
}

+ (CGRect) convertRect:(CGRect)rect toView:(UIView *)view {
    UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *) view : [view window];
    return [view convertRect:[window convertRect:rect fromWindow:nil] fromView:nil];
}

It works, until I push another view on top of the current view, then pop it back. Then I cannot see my UITextField when editing them.

Here's what the NSLog above say:

  • Before pushing (works):

    detailView bounds: 0, (-704 + 352 x -1), 703, 768
    
  • After popping (not):

    detailView bounds: 0, (352 + 352 x 1), 703, 768
    

So it seems my UIViewController loses its self.view.frame.origin.y between push/pops...

In IB, all the sizes of both views (detailView and pushedView) are set to "inferred". The following options are also checked identically for both views:

  • Adjust Scroll View Insets
  • Hide Bottom Bar on Push
  • Extend Edges: Under Top Bars
  • " " : Under Bottom Bars
  • " " : Under Opaque Bars

So, what's happening to my view? Thanks for your help.

役に立ちましたか?

解決

As always, it's about how the observers are added: They should not be added in the viewDidLoadmethod but in the viewWillAppear. It's fixed now.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top