2014-09-21 16 views
5

के साथ आईपैड पर कीबोर्ड दिखाई देने पर एक सामान्य रूप से प्रस्तुत UIViewController को ऊपर ले जाना, मुझे एक UINiewController के अंदर UIViewController मिला है, जो आईओएस 8 के साथ एक आईपैड पर मॉड्यूल रूप से प्रस्तुत किया गया है। मैंआईओएस 8

self.myNavController.modalPresentationStyle = UIModalPresentationFormSheet; 

मेरे दृश्य नियंत्रक के भीतर, मेरे पास एक टेक्स्ट फ़ील्ड है। व्यू कंट्रोलर के यूआई डिज़ाइन के कारण, जब उपयोगकर्ता टेक्स्ट फ़ील्ड में टैप करता है, और कीबोर्ड दिखाई देता है, तो मुझे स्क्रीन पर व्यू कंट्रोलर को स्थानांतरित करने की आवश्यकता होती है। iOS7 पर, मैं आदेश दृश्य नियंत्रक ऊपर ले जाने के, और वापस नीचे में

self.myNavController.view.superview.frame.origin 

में होने वाले बदलाव एनिमेट किया गया था। यह अच्छी तरह से काम किया।

हालांकि, आईओएस 8 पर, यह अब काम नहीं कर रहा है। वास्तव में, अजीब तरह से, व्यवहार दृश्य नियंत्रक के लिए एक छोटे मूल्यवान वाई समन्वय के साथ थोड़ा नीचे स्थानांतरित करने के लिए और उसके बाद का बैक अप (सभी जब मैं self.myNavController.view.superview.frame.origin बताए कर रहा हूँ एक CGPoint है)।

मूल रूप से, मैं एक सुझाव यह में दिए गए से काम कर रहा था अतः Resize ModalViewController and position it at center in iOS 7

सवाल और मैं हाल ही में सुझाव Moving dialog up when keyboard present works except when ipad is turned around में लेकिन इसका कोई लाभ के लिए दिया की कोशिश की। असल में, वहां एक उत्तर में परिवर्तन विचार के साथ, मैं कम से कम सही दिशा में जाने के लिए दृश्य नियंत्रक प्राप्त कर सकता हूं (यानी, ऊपर), लेकिन यह वहां नहीं रहता है। यह जल्दी से नीचे चला जाता है।

विचार?

उत्तर

2

हम करेंगे, अब मैं काफी परेशान हूं। मैंने यह पता लगाया है। इसे नए, आईओएस 8 यूआईपी प्रतिनिधित्व कंट्रोलर का उपयोग करके हल किया जा सकता है। मैं नीचे कुछ नमूना कोड दूंगा। और बाद में एक कार्यरत परियोजना ज़िप फ़ाइल में लिंक करने का प्रयास करें।

पहला दृश्य नियंत्रक उदाहरण ऐप का मूल दृश्य नियंत्रक है। ChangeFrameTransitioningDelegate के लिए कोड नीचे दिए गए हैं। "संवाद" सिर्फ एक खाली दृश्य नियंत्रक वर्ग है।

// 
// ViewController.m 
// TestPresentationController 
// 
// Created by Christopher Prince on 9/21/14. 
// Copyright (c) 2014 Spastic Muffin, LLC. Use at your own discretion. 
// 

#import "ViewController.h" 
#import "ChangeFrameTransitioningDelegate.h" 
#import "Dialog.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void) viewDidAppear:(BOOL)animated; 
{ 
    [super viewDidAppear:animated]; 

    ChangeFrameTransitioningDelegate *td = [ChangeFrameTransitioningDelegate new]; 
    td.viewControllerFrame = CGRectMake(100, 0, 100, 100); 

    Dialog *d = [Dialog new]; 
    // Just to make the VC stand out. 
    d.view.backgroundColor = [UIColor greenColor]; 

    // Need to make this custom in order to have the presentationControllerForPresentedViewController method of the transitioning delegate called. 
    d.modalPresentationStyle = UIModalPresentationCustom; 

    d.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    d.transitioningDelegate = td; 

    [self presentViewController:d animated:YES completion:^{ 

     // Any time after the view controller is presented you can change the 
     // viewControllerFrame property to change the size and/or position of the 
     // presented view controller. 
     [UIView animateWithDuration:0.5 animations:^{ 
      td.viewControllerFrame = CGRectMake(100, 800, 100, 100); 
     }]; 
    }]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
// 
// ChangeFrameTransitioningDelegate.h 
// TestPresentationController 
// 
// Created by Christopher Prince on 9/21/14. 
// Copyright (c) 2014 Spastic Muffin, LLC. Use at your own discretion. 
// 

// For iOS8 and later, use this to change the size/position of a view controller after it has been presented. See TextPresentationController example. 
#import <Foundation/Foundation.h> 
#import <UIKit/UIKit.h> 

@interface ChangeFrameTransitioningDelegate : NSObject<UIViewControllerTransitioningDelegate> 

// This is the size and position that the PresentationController used by this class will use when its frameOfPresentedViewInContainerView method is called. So, this is the size and position that your view controller will have. 
@property (nonatomic) CGRect viewControllerFrame; 

@end 
// 
// ChangeFrameTransitioningDelegate.m 
// TestPresentationController 
// 
// Created by Christopher Prince on 9/21/14. 
// Copyright (c) 2014 Spastic Muffin, LLC. Use at your own discretion. 
// 

// Some help from http://stackoverflow.com/questions/25903412/editing-bounds-of-uiview-when-presenting-hides-keyboard-in-ios-8 
// http://stackoverflow.com/questions/25811199/ios-8-change-the-size-of-presented-modal-view-controller 
// Some code from https://developer.apple.com/devcenter/download.action?path=/wwdc_2014/wwdc_2014_sample_code/lookinsidepresentationcontrollersadaptivityandcustomanimatorobjects.zip (though, beware, this example doesn't work straight out of the box). 
// And see http://stackoverflow.com/questions/25957343/moving-a-modally-presented-uiviewcontroller-up-when-keyboard-appears-on-ipad-wit 

#ifndef SPASLogDetail 
#define SPASLogDetail NSLog 
#endif 

#import "ChangeFrameTransitioningDelegate.h" 

@interface PresentationController : UIPresentationController 
{ 
    UIView *_dimmingView; 
} 
// This is the size and position that the PresentationController will use when its frameOfPresentedViewInContainerView method is called. 
@property (nonatomic) CGRect currentFrame; 
@end 

@implementation PresentationController 

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController; 
{ 
    self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController]; 
    // Some random default size. 
    self.currentFrame = CGRectMake(0, 0, 100, 100); 
    [self prepareDimmingView]; 
    return self; 
} 

// 4/22/15; Added, for rotation. 
- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator; 
{ 
    CGSize increasedSize = size; 
    // 4/22/15; I'm getting some odd effects: In a rotation transition from landscape to portrait, the RHS goes white momentarily, and from portrait to landscape, the bottom goes white momentarily. A hack, but increase the size of the dimming view to get around this. 
    increasedSize.width *= 1.5; 
    increasedSize.height *= 1.5; 
    _dimmingView.frameSize = increasedSize; 

    // There appears to be no transitionCoordinator available for this "transition". Therefore using the regular UIView animation, below. 
    /* 
    if([self.presentedViewController transitionCoordinator]) 
    { 
     [[self.presentedViewController transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { 
      recenterViewController(); 
     } completion:nil]; 
    } 
    else 
    { 
     recenterViewController(); 
    }*/ 

    // 4/23/15; See [1]. Given that I've turned off resizing with the autoresizingMask, this has messed up the repositioning of the (x, y) coordinate of the presentedViewController. Make up for that here. 
    [UIView animateWithDuration: coordinator.transitionDuration animations:^{ 
     // I'm assuming the size here refers to the entire window. 
     CGPoint center = CGPointMake(size.width/2.0, size.height/2.0); 
     self.presentedViewController.view.center = center; 
    } completion:nil]; 
} 

- (void)presentationTransitionWillBegin 
{ 
    UIView* containerView = [self containerView]; 

    // 4/23/15; So, this seems to be related to my modal view resizing problem. When I disable autoresizesSubviews, I stop the resizing during the rotation. HOWEVER, this is at the price of two additional problems: (a) the (x, y) coordinate of the modal stops being changed during the rotation, and (b) the animation of the background (_dimmingView?) starts looking funky during the rotation. WHICH makes sense. The containerView size is the full window. 
    //containerView.autoresizesSubviews = NO; 

    UIViewController* presentedViewController = [self presentedViewController]; 

    // [1] 4/23/15; This is better. The _dimmingView animation is preserved. The (x, y) position of the presentedViewController, however isn't right after the rotation. 
    presentedViewController.view.autoresizingMask = UIViewAutoresizingNone; 

    [_dimmingView setFrame:[containerView bounds]]; 
    [_dimmingView setAlpha:0.0]; 

    [containerView insertSubview:_dimmingView atIndex:0]; 

    void (^setAlpha)() = ^{ 
     [_dimmingView setAlpha:1.0]; 
    }; 

    if([presentedViewController transitionCoordinator]) 
    { 
     [[presentedViewController transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { 
      setAlpha(); 
     } completion:nil]; 
    } 
    else 
    { 
     setAlpha(); 
    } 
} 

- (void)dismissalTransitionWillBegin 
{ 
    void (^resetAlpha)() = ^{ 
     [_dimmingView setAlpha:0.0]; 
    }; 

    if([[self presentedViewController] transitionCoordinator]) 
    { 
     [[[self presentedViewController] transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { 
      resetAlpha(); 
     } completion:nil]; 
    } 
    else 
    { 
     resetAlpha(); 
    } 
} 

- (CGRect)frameOfPresentedViewInContainerView 
{ 
    SPASLogDetail(@"frameOfPresentedViewInContainerView: %@", NSStringFromCGRect(self.currentFrame)); 
    return self.currentFrame; 
} 

- (void) setCurrentFrame:(CGRect)currentFrame 
{ 
    _currentFrame = currentFrame; 
    // This is the important part for changing the frame of the presented view controller *after* the view is presented. For some odd reason, its not right to change the frame of the containerView. 
    self.presentedView.frame = currentFrame; 
} 

- (void) containerViewWillLayoutSubviews; 
{ 
    SPASLogDetail(@"containerViewWillLayoutSubviews"); 
} 

- (void)prepareDimmingView 
{ 
    _dimmingView = [[UIView alloc] init]; 
    [_dimmingView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]]; 
    [_dimmingView setAlpha:0.0]; 

    //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dimmingViewTapped:)]; 
    //[_dimmingView addGestureRecognizer:tap]; 
} 

/* 
- (void)dimmingViewTapped:(UIGestureRecognizer *)gesture 
{ 
    if([gesture state] == UIGestureRecognizerStateRecognized) 
    { 
     [[self presentingViewController] dismissViewControllerAnimated:YES completion:NULL]; 
    } 
}*/ 

@end 

@interface ChangeFrameTransitioningDelegate() 
@property (nonatomic, strong) PresentationController *presentationController; 
@end 

@implementation ChangeFrameTransitioningDelegate 

- (instancetype) init; 
{ 
    self = [super init]; 
    return self; 
} 

- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source 
{ 
    self.presentationController = [[PresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting]; 
    self.presentationController.currentFrame = self.viewControllerFrame; 

    return self.presentationController; 
} 

- (void) setViewControllerFrame:(CGRect)viewControllerFrame 
{ 
    _viewControllerFrame = viewControllerFrame; 
    self.presentationController.currentFrame = viewControllerFrame; 
} 

@end 
+0

Hmmmm। अब मैं इसके साथ एक समस्या देख रहा हूँ। यह डिवाइस घूर्णन के साथ आता है। मॉडल संवाद आयाम तब संरक्षित नहीं होते हैं जब मोडल प्रदर्शित होता है और आप डिवाइस को घुमाते हैं। –

+0

मैंने अभी कुछ बदलाव जोड़े हैं। कोड में हाल ही में दिनांकित टिप्पणियां देखें। मुझे लगता है कि मैंने इसे ठीक कर दिया है। –