Transparent ViewController to See Parent Below?

后端 未结 9 844
自闭症患者
自闭症患者 2021-02-01 18:59

I would like to modally add a view controller with a transparent background, so the parent view controller beneath can be seen. (This is in an app for iPhone, not for iPad.)

相关标签:
9条回答
  • 2021-02-01 19:35

    For completeness and having it up-to-date, I am also adding the solution for Swift:

    either

    viewController.modalPresentationStyle = .CurrentContext 
    

    being - A presentation style where the content is displayed over only the presenting view controller’s content.

    or

    viewController.modalPresentationStyle = .OverCurrentContext
    

    being - A presentation style where the content is displayed over only the parent view controller’s content. The views beneath the presented content are not removed from the view hierarchy when the presentation finishes. So if the presented view controller does not fill the screen with opaque content, the underlying content shows through.

    Depending on the requirements and circumstances for the presentation.

    0 讨论(0)
  • 2021-02-01 19:38

    Answer for Swift 5.

    TransparentViewController

    self.view.backgroundColor = .clear
    

    PresentingViewController

    let viewController = TransparentViewController()
    viewController.modalPresentationStyle = .overFullScreen
    navigationController.present(viewController, animated: false)
    
    0 讨论(0)
  • 2021-02-01 19:40

    It's possible although there are a bunch of steps which can be easily forgotten. After struggling with myself to get this working for 4 hours I came up with the following solution.

    1 - Create a View Controller like any other.

    Header file

    #import "DDBaseViewController.h"
    
    @interface DDHomeSearchLoadingViewController : UIViewController
    
    @end
    

    Implementation file

    #import "DDHomeSearchLoadingViewController.h"
    
    @interface DDHomeSearchLoadingViewController ()
    @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityMonitor;
    @property (weak, nonatomic) IBOutlet UIView *modalView;
    
    @end
    
    @implementation DDHomeSearchLoadingViewController
    
    #pragma mark - UIViewController lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
    
        [self setupUI];
    }
    
    -(void) setupUI
    {
        [self makeRoundedCorner:self.modalView AndCornerRadius:6.0f];
        [self.activityMonitor startAnimating];
    
    }
    
    -(void) makeRoundedCorner:(UIView*) view AndCornerRadius:(float) cornerRadius
    {
        [view.layer setCornerRadius:cornerRadius];
        [view.layer setMasksToBounds:YES];
    }
    
    @end
    

    2 - Set your container view background color as ClearColor

    enter image description here

    3 - Add a UIView which will be presented like an overlay

    enter image description here

    4 - Add a UIView which will be presented like a Dialog Box over the overlay UIView

    Make sure it's outside the overlay view when you add/move it. For some reason when you move it using the mouse it adds to overlay UIView automatically. ( Freaking annoying to be honest )

    enter image description here

    5 - Set the Storyboard ID for the View you've created

    enter image description here

    6 - Finally add this piece of code wherever you wan't call it ( Assuming it's a UiViewController too )

    DDHomeSearchLoadingViewController* ddHomeSearchLoadingViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"DDHomeSearchLoadingViewController"];
    self.navigationController.modalPresentationStyle = UIModalPresentationCurrentContext;
    [self presentViewController:ddHomeSearchLoadingViewController animated:YES completion:nil];
    

    I hope it helps you guys out

    Cheers

    0 讨论(0)
  • 2021-02-01 19:43

    you can make changes in storyboard just by doing below options and also reducing parental view opacity. Using Storyboard: No need to write any code to achieve this

    0 讨论(0)
  • 2021-02-01 19:45

    I have been searching for the solution. Now thanks to iOS 8. They has introduced couple of new modalPresentationStyle. one among them is UIModalPresentationOverCurrentContext. Used the same to solve the this issue.

    viewcontroller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    

    Hope this helps.

    0 讨论(0)
  • 2021-02-01 19:46
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
        ChooseController *sec=[story instantiateViewControllerWithIdentifier:@"Controller"];
        sec.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentViewController:sec animated:YES completion:^{
    
        }];
    

    Note:Present controller superview alpha value must be below 1 like 0.5 alpha like that.

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