Putting a modal view on the presentation and creating a background gray

I have been trying to do this for the last 3 hours, and I could not figure out how to do this. Can anyone PLEASE help?

So this is what I am trying to do. When I press a button, say a button Sign In, I want a modal view to appear that makes it look gray and invulnerable. And in this modal view, I want some buttons and static labels.

I read and tried to understand several resources, for example: Introduce a modal view controller in a half-size parent controller , http://makeapppie.com/2014/08/30/the-swift-swift-tutorials-adding-modal-views-and- popovers / ; How to use modal representations in swift? and several others. However, it’s so hard for me to understand the code.

So far, I have this code that should make a modal view on top of the view behind it:

@IBAction func signIn(sender: AnyObject) {
    self.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    // Cover Vertical is necessary for CurrentContext
    self.modalPresentationStyle = .CurrentContext
    // Display on top of    current UIView
    self.presentViewController(SignInViewController(), animated: true, completion: nil)
}

But this does not create the effect that I want. Can someone please help?

0
source share
3 answers

First create your gray blank view

func makeGrayView() -> UIView {
    var view = UIView(frame:UIScreen.mainScreen().applicationFrame)
    self.view.backgroundColor = UIColor.greyColor()
    return view
}

-,

var backView = self.makeGrayView()
self.view.addSubview(backView)
0

Cusotm UIViewControllerAnimation. .Create TransparentTransition.h and .m File

#import <Foundation/Foundation.h>

@interface TransparentTransition :NSObject<UIViewControllerAnimatedTransitioning,UIViewControllerTransitioningDelegate>{   
BOOL isPresenting;
}
@end

//TransparentTransition.m

#import "TransparentTransition.h"

@implementation TransparentTransition
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{
return 0.8f;
}

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

UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView *toView = toVC.view;
UIView *fromView = fromVC.view;

UIView* containerView = [transitionContext containerView];


// Position the presented view off the top of the container view

if(isPresenting){
   [containerView addSubview:toView];
    toView.frame = CGRectMake(0, -toView.frame.size.height, toView.frame.size.width, toView.frame.size.height);
    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0
         usingSpringWithDamping:.8
          initialSpringVelocity:6.0
                        options:0
                     animations:^{
                          toView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"black_patch.png"]];
                         CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(1.0f, 1.0f);
                         CGAffineTransform lefttorightTrans  = CGAffineTransformMakeTranslation(0.f,+toView.frame.size.height);
                         toView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans);


                     }
                     completion:^(BOOL finished){
                         [transitionContext completeTransition:YES];

                     }];

}
else{

    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                          delay:0
                        options:0
                     animations:^{
                         //                              toView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"black_patch.png"]];
                         CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(1.0f, 1.0f);
                         CGAffineTransform lefttorightTrans  = CGAffineTransformMakeTranslation(0,-fromView.frame.size.height);
                         fromView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans);


                     }
                     completion:^(BOOL finished){
                         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
                     }];


    }

   }



- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{

isPresenting=YES;
return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
isPresenting=NO;
return self;
}

- (UIDynamicAnimator*)animateForTransitionContext:(id<UIViewControllerContextTransitioning>)transitionContext {
// Has to be implemented by subclasses
return nil;
}

//

var transitionManager:TransparentTransition
@IBAction func signIn(sender: AnyObject) {

let detail = self.storyboard?.instantiateViewControllerWithIdentifier("detail") as! DetailViewController
    detail.modalPresentationStyle = UIModalPresentationStyle.Custom;
    detail.transitioningDelegate = transitionManager;
self.presentViewControllerdetail, animated: true, completion: nil)
}

, , ​​ http://www.appcoda.com/custom-view-controller-transitions-tutorial/

0

, . UIViewController. enter image description here

@IBAction func presentForm() {
    self.view.backgroundColor = UIColor.lightGrayColor()
}

-, UIViewController Ctrl , segue. , . . enter image description here

Ok ViewController. . . " ". , "". enter image description here

Then drag another top view where the form elements will be. Like the shortcut and text fields that I have. When you create and run a project, you will have the following screens.enter image description here enter image description here

Note that you will need to add your own layout constraints, but they are simple. You can also play with the code. Like changing the Opaque value of the first Viewcontroller on a button, click, etc. That should be enough to get you started.

0
source

Source: https://habr.com/ru/post/1608646/


All Articles