Present a modal view controller with transparent background

后端 未结 1 572
醉酒成梦
醉酒成梦 2020-12-03 05:52

I want to present a UIViewController programmatically which should show up (or not, rather) with transparent background. I want it for iOS 7.0 and above. I\'ve

相关标签:
1条回答
  • 2020-12-03 06:46

    Suppose, we're in FirstViewController

    //Obj-C
    - (void) presentSecondVC {
        SecondViewController *vc = [[SecondViewController alloc] init];
        [self addChildViewController:vc];
        [self didMoveToParentViewController:vc];
    }
    
    //Swift
    func presentSecondVC() {
        let vc = SecondViewController.init()
        self.addChildViewController(vc)
        self.didMove(toParentViewController: vc)
    }
    

    Some of you may need to write above method like this,

    //Obj-C
    - (void) presentSecondVC {
        SecondViewController *vc = [[SecondViewController alloc] init];
        vc.view.frame = CGRectMake(0,0,width,height); //Your own CGRect
        [self.view addSubview:vc.view]; //If you don't want to show inside a specific view
        [self addChildViewController:vc];
        [self didMoveToParentViewController:vc];
        //for someone, may need to do this.
        //[self.navigationController addChildViewController:vc];
        //[self.navigationController didMoveToParentViewController:vc];   
    }
    
    //Swift
    func presentSecondVC() {
        let vc = SecondViewController.init()
        vc.view.frame = CGRect.init(x: 0, y: 0, width: width, height: height) //Your own CGRect
        self.view.addSubview(vc.view) //If you don't want to show inside a specific view.
        self.addChildViewController(vc)
        self.didMove(toParentViewController: vc)
        //for someone, may need to do this.
        //self.navigationController?.addChildViewController(vc)
        //self.navigationController?.didMove(toParentViewController: vc)
    }
    

    Now in SecondViewController when you want to go back

    //Obj-C
    - (void) goBack {
        [self removeFromParentViewController];
    }
    
    //Swift
    func goBack() {
        self.removeFromParentViewController()
    }
    

    Do play well (with each scenario) :)

    And yes, this will not show an animation, in my case, I'm showing a custom popup inside vc though it looks nice with this code!

    0 讨论(0)
提交回复
热议问题