iOS : ModalView with background transparent?

前端 未结 6 893
慢半拍i
慢半拍i 2020-12-24 02:14

I want to show a modalview on a viewController. (which has a naviguation controller).

On my view i have text, and a button to show the modalview.

I created

相关标签:
6条回答
  • 2020-12-24 02:33

    you can check the iOS7 example (see my comm) or you can simple try this:

    remove this line from your "presenting" method

    controller.view.backgroundColor = [UIColor clearColor];
    

    now, in viewDidLoad of the ShareController add:

     self.view.backgroundColor = [UIColor clearColor];
     self.modalPresentationStyle = UIModalPresentationCurrentContext;
     self.modalPresentationStyle = UIModalPresentationFormSheet;
    

    PS

    if you have a navigationController... use

    [self.navigationController presentViewController:controller animated:YES completion:nil];
    
    0 讨论(0)
  • 2020-12-24 02:33

    The quick answer is you cannot present transparent modal view, not with presentViewController:animated:completion: method. Because you cannot make the modal view controller transparent (your view is placer on top of that).

    You can make custom view and you can manually animate it, this is a way to create something what you need.

    0 讨论(0)
  • 2020-12-24 02:43

    Use following snippet to do it on iOS 8 onwards:

    For Objective C:

    UIViewController *walkThru = [self.storyboard   instantiateViewControllerWithIdentifier:@"WalkThroughScene"];
    walkThru.providesPresentationContextTransitionStyle = YES;
    walkThru.definesPresentationContext = YES;
    [walkThru setModalPresentationStyle:UIModalPresentationOverCurrentContext];
    [self.navigationController presentViewController:walkThru animated:YES completion:nil];
    

    For Swift 2 :

    let viewController : XYZViewController =     self.storyboard!.instantiateViewControllerWithIdentifier(“XYZIdentifier”) as! XYZViewController
    viewController.providesPresentationContextTransitionStyle = true
    viewController.definesPresentationContext = true
    viewController.modalPresentationStyle=UIModalPresentationStyle.OverCurrentContext
    self.presentViewController(viewController, animated: true, completion: nil)
    

    For Swift 4 :

    let viewController =  self.storyboard!.instantiateViewController(withIdentifier:  "XYZIdentifier") as! XYZViewController
    viewController.providesPresentationContextTransitionStyle = true
    viewController.definesPresentationContext = true
    viewController.modalPresentationStyle = .overCurrentContext
    self.present(viewController, animated: true, completion: nil)
    

    And the backgroundcolor of your presented viewController should be clearColor.

    0 讨论(0)
  • 2020-12-24 02:43

    That is the UIWindow's color,you can init the UIWindow color in appDelegate.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        self.window.backgroundColor = [UIColor whiteColor];
        return YES;
    }
    
    0 讨论(0)
  • 2020-12-24 02:46

    In the PresentingViewController copy the below code under storyboard segue or IBAction.

    SecondViewController *viewController = [self.storyboard   instantiateViewControllerWithIdentifier:@"secondViewController"];
    //SecondViewController *viewController = [SecondViewController alloc]init]; // if you are adding the viewcontroller programatically
    viewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        [viewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:viewController animated:YES completion:nil];
    

    In the second ViewController do the following steps either by storyboard or through code:

    1. Set the view's (self.view) backgroundcolor to clearcolor and reduce the opacity to 50 %
    2. Now you can realize a semi transparent modal view
    0 讨论(0)
  • 2020-12-24 02:48

    If you want your modalVC to be over the tabbar you need to define the presentation style as UIModalPresentationOverFullScreen

    [_presentedViewController setModalPresentationStyle:UIModalPresentationOverFullScreen];
    
    [_presentingViewController presentViewController:_presentedViewController animated:YES completion:nil];
    
    0 讨论(0)
提交回复
热议问题