można przedstawić kontroler widok, a jeszcze oryginalny kontroler widoku widoczny pod spodem, jak forma, w iOS 7. Aby to zrobić, trzeba zrobić dwie rzeczy :
Ustaw modalne styl prezentacji zwyczaju:
viewControllerToPresent.modalPresentationStyle = UIModalPresentationCustom;
Ustaw delegat przenoszący:
viewControllerToPresent.transitioningDelegate = self;
W tym przypadku musimy ustawić delegata do siebie, ale może być inny obiekt. Delegat musi realizować dwa wymagane metody protokołu, możliwe tak:
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
SemiModalAnimatedTransition *semiModalAnimatedTransition = [[SemiModalAnimatedTransition alloc] init];
semiModalAnimatedTransition.presenting = YES;
return semiModalAnimatedTransition;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
SemiModalAnimatedTransition *semiModalAnimatedTransition = [[SemiModalAnimatedTransition alloc] init];
return semiModalAnimatedTransition;
}
W tym momencie można myśleć, gdzie zrobił SemiModalAnimatedTransition
klasa pochodzą. Cóż, jest to niestandardowa implementacja przyjęta z bloga teehan+lax.
Oto nagłówek klasy za:
@interface SemiModalAnimatedTransition : NSObject <UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) BOOL presenting;
@end
i wdrożenie:
#import "SemiModalAnimatedTransition.h"
@implementation SemiModalAnimatedTransition
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return self.presenting ? 0.6 : 0.3;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
CGRect endFrame = fromViewController.view.bounds;
if (self.presenting) {
fromViewController.view.userInteractionEnabled = NO;
[transitionContext.containerView addSubview:fromViewController.view];
[transitionContext.containerView addSubview:toViewController.view];
CGRect startFrame = endFrame;
startFrame.origin.y = endFrame.size.height;
toViewController.view.frame = startFrame;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
toViewController.view.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
else {
toViewController.view.userInteractionEnabled = YES;
[transitionContext.containerView addSubview:toViewController.view];
[transitionContext.containerView addSubview:fromViewController.view];
endFrame.origin.y = endFrame.size.height;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
fromViewController.view.frame = endFrame;
} completion:^(BOOL finished) {
[transitionContext completeTransition:YES];
}];
}
}
@end
Nie najprostszym rozwiązaniem, ale unika hacki i działa dobrze.Niestandardowe przejście jest wymagane, ponieważ domyślnie iOS usunie pierwszy kontroler widoku na końcu przejścia.
UPDATE:
Dla iOS 8, po raz kolejny zmieniła krajobraz. Wszystko, co musisz zrobić, to użyć nowej prezentacji styl .OverCurrentContext
, a mianowicie:
viewControllerToPresent.modalPresentationStyle = UIModalPresentationOverCurrentContext;
To nie jest problem przejrzystości. Po zakończeniu animacji system iOS usuwa kontroler ukrytych widoków z ekranu. Czarna, którą widzisz, to kolor tła okna. Wierzę, że iOS7 ma kilka opcji. –
@BrianNickel Masz na myśli, że kontroler podglądu prezentera jest ukryty, dopóki nie zostanie odrzucony? W takim przypadku, czy powinienem dodać widok ręcznie i użyć animacji, aby przenieść ją z dołu ekranu? – frankish
To prawda. Możesz pominąć 'menuViewController' i po prostu animować' menuView' na ekranie, jak tylko chcesz. –