在iOS 9中呈现透明模态视图

时间:2016-02-08 11:14:33

标签: ios objective-c modalviewcontroller

有很多答案,但似乎没有一个正常/完美适用于iOS 9特别是9.2 ..有人请帮忙。这是一个简单的场景:

1)故事板segue连接到一个toBePresentedModally View Controller,我选择UIModalPresentationOverCurrentContext以及当故事板的样式不起作用时切换并尝试使用UIModalPresentationCurrentContext。

2)我要么在幸福1秒钟后获得黑色背景(它只能工作一秒钟)或者我试图通过代码分配一组toBePresentedModally View Controller来获得一个空白的控制器。

任何帮助将不胜感激!

5 个答案:

答案 0 :(得分:2)

这是我在代码中完成的方式

let viewController = UIStoryboard.loadViewController("Identifier")
viewController.providesPresentationContextTransitionStyle = true
viewController.definesPresentationContext = true
viewController.modalPresentationStyle = .OverCurrentContext
self.presentViewController(viewController)

它工作正常。

答案 1 :(得分:1)

以下是在iOS 9 +中测试的正确答案:

使用故事板

在属性检查器中选择您的segue:

  • 种类"模态"
  • 演示和过渡"默认"

enter image description here

之后,选择您的模态视图控制器并在属性检查器中:

  • 演示文稿"目前的背景情况"

enter image description here

希望可以帮助你。

答案 2 :(得分:0)

实现类“TransitionDelegate”,它符合协议UIViewControllerTransitioningDelegate并为

提供实现
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    AnimatedTransitioning *controller = [[AnimatedTransitioning alloc]init];
    controller.isPresenting = YES;
    return controller;
}

您的AnimatedTransition类应该看起来像这样,

#import "AnimatedTransitioning.h"
#import "MainViewController.h"
#import "SecondViewController.h"

@implementation AnimatedTransitioning

//===================================================================
// - UIViewControllerAnimatedTransitioning
//===================================================================

- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.25f;
}

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {

    UIView *inView = [transitionContext containerView];
    SecondViewController *toVC = (SecondViewController *)[transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    MainViewController *fromVC = (MainViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];

    [inView addSubview:toVC.view];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    [toVC.view setFrame:CGRectMake(0, screenRect.size.height, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];

    [UIView animateWithDuration:0.25f
                     animations:^{

                         [toVC.view setFrame:CGRectMake(0, 0, fromVC.view.frame.size.width, fromVC.view.frame.size.height)];
                     }
                     completion:^(BOOL finished) {
                         [transitionContext completeTransition:YES];
                     }];
}
@end

这里我给出了源视图控制器类(MainViewController.m)中按钮操作的实现,我希望你可以根据你的要求进行更改,

- (IBAction)displaySecondVC:(id)sender {

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    vc.view.backgroundColor = [UIColor clearColor];
    vc.modalPresentationStyle= UIModalPresentationCustom;
    [vc setTransitioningDelegate:transitionController];
    [self presentViewController:vc animated:YES completion:nil];
}

在同一个类的viewdidLoad中添加此

self.transitionController = [[TransitionDelegate alloc] init];

请与我联系以获得任何澄清。

答案 3 :(得分:0)

如果你想为ViewController提供透明背景,试试这个!!

  • 选择一个视图(命名为 DimmedView ),并将其设置为与superView相等的宽度和高度。
  • 将SuperView的背景颜色设置为 ClearColor ,将DimmedView的 LightGrayColor 设置为 alpha 0.5
  • 现在使用您的实际内容获取CenterView并设置适当的约束。
  • 如果您想在CenterView外部触发控制器,请添加点按手势到DimmeedView
  • 现在,当您呈现viewController时,设置此属性。

    while read -r line; do [[ ! $line =~ [0-9] ]] && printf "%s\n" "$line"; done < file
    
  • 不要忘记在TapGesture事件上解散ViewController。

enter image description here

答案 4 :(得分:0)

DashboardSettingsViewController *view=[self.storyboard   instantiateViewControllerWithIdentifier:@"dashboardSettings"];

view.providesPresentationContextTransitionStyle = YES;
view.definesPresentationContext = YES;
[view setModalPresentationStyle:UIModalPresentationOverCurrentContext];
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
transition.timingFunction =
[CAMediaTimingFunction   functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;


//// for a smoother navigation
UIView *containerView = self.view.window;
[containerView.layer addAnimation:transition forKey:nil];
[self.navigationController presentViewController:view animated:YES completion:nil];

// lastly the background of to be presented view controller should be set to clear with alpha of your choice!