iOS: Why do I have white, rounded corners on my modal view?

前端 未结 9 746
悲哀的现实
悲哀的现实 2021-01-02 00:37

I have a modal view popping up in my iPad app and for some reason it has white, rounded corners.

It might be worth noting I built this model view in my storyboard, n

相关标签:
9条回答
  • 2021-01-02 00:46

    Try

    [self.view superview].layer.cornerRadius = 21.0f;
    [self.view superview].layer.masksToBounds = YES;
    
    0 讨论(0)
  • 2021-01-02 00:50

    I agree with @PeterPurple's answer, but it took me some time to come up with code that actually worked. My example displays a modal view that is centered horizontally, but put more toward the top of the screen so the view doesn't obstruct the keyboard. Of course I could just move the view up when the keyboard shows, but I'm lazy. Anyhow, here's how I did it.

    I created a custom modal segue and in that segue I overrode the perform: method as so:

    @implementation CustomSegue
    
    static const CGFloat TOP_MARGIN = 40;
    static const CGFloat SUPER_VIEW_GAP = 10;
    
    - (void)perform {
        UIView *destView = [self.destinationViewController view];
    
        [self.destinationViewController setModalPresentationStyle:UIModalPresentationPageSheet];
        [self.destinationViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [[self sourceViewController] presentViewController:[self destinationViewController] animated:YES completion:nil];
    
        CGFloat x, y, width, height, yCenter;
        CGSize contentSize = [self.destinationViewController contentSizeForViewInPopover];
    
        x = destView.superview.frame.origin.x;
        y = destView.superview.frame.origin.y;
        width = contentSize.width;
        height = contentSize.height;
        yCenter = (height / 2.0) + TOP_MARGIN;
    
        destView.superview.frame = CGRectMake(x + SUPER_VIEW_GAP, y + SUPER_VIEW_GAP, width - (SUPER_VIEW_GAP * 2), height - (SUPER_VIEW_GAP * 2));
        destView.superview.autoresizingMask = UIViewAutoresizingNone;
        destView.superview.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
        destView.superview.center = CGPointMake([[self.sourceViewController view] center].x, yCenter);
        destView.frame = CGRectMake(-SUPER_VIEW_GAP, -SUPER_VIEW_GAP, width, height);
    }
    
    0 讨论(0)
  • 2021-01-02 00:52

    If you are using a popoverpresentationcontroller to show your view controller, try setting the backgroundcolor of the popoverpresentationcontroller to clear

        examplePopUpController = exampleViewController.popoverPresentationController!
        examplePopUpController?.backgroundColor = UIColor.clear
    
    0 讨论(0)
  • 2021-01-02 00:58

    Try in app delegate: [[self window] setBackgroundColor:[UIColor blackColor]];

    0 讨论(0)
  • 2021-01-02 01:00

    I realize this is an old question but it deserves an answer...

    On the iPad Modal views (using UIModalPresentationFormSheet) have a default background which is a white bordered image with whitish dog eared corners. Anyway the default background will show through if your modal view background has round or transparent corners.

    I spent a fair bit of time trying to figure out how to have my background view show without having the default background show. The trick is to make the superview smaller than your view:

    vc.view.clipsToBounds = NO;
    vc.view.superview.bounds = CGRectMake(vc.view.superview.bounds.origin.x, vc.view.superview.bounds.origin.y, 300, 480);
    

    NOTES: the vc is the view you are presenting. Don't make the view too small or else your touch event will not work.

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

    Your question is ambiguous about what kind of presentation you're using for your view controller, so I'm going to assume you're using a form sheet. The solution is to set the superview's background color to [UIColor clearColor] to prevent the translucent background from appearing:

    - (void) viewDidAppear:animated
    {
        [super viewDidAppear:animated];
    
        self.view.layer.cornerRadius = 10;
        self.view.layer.masksToBounds = YES;
        self.view.superview.backgroundColor = [UIColor clearColor];
    }
    

    Before setting backgroundColor:

    Before

    After setting backgroundColor:

    After

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