【问题标题】:iOS: Why do I have white, rounded corners on my modal view?iOS:为什么我的模态视图上有白色的圆角?
【发布时间】:2012-10-19 00:07:06
【问题描述】:

我的 iPad 应用程序中弹出了一个模态视图,由于某种原因,它有白色的圆角。

值得注意的是,我在故事板中构建了这个模型视图,而不是以编程方式。但是,在我的 viewWillAppear 方法中,我正在像这样设置角半径......

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.view.layer.cornerRadius = 6.0f;
}

当我将值设置为 6 以上时,白色角变得可见。如何在不显示这些白色圆角的情况下将值设置得更高?

非常感谢您的智慧!

【问题讨论】:

  • 这是发生在模拟器上还是真机上?
  • @Rickay,在模拟器上。我还没有在设备上试用过,但明天我可以使用设备时会尝试。
  • @lnafziger,是的。然而,改变它似乎没有任何效果
  • 好吧,如果它是不透明的,那么它就不会通过它显示背景......

标签: iphone ios ipad


【解决方案1】:

您的问题是关于您为视图控制器使用哪种演示文稿的问题含糊不清,因此我假设您使用的是表单。解决方法是将superview的背景色设置为[UIColor clearColor],防止出现半透明背景:

- (void) viewDidAppear:animated
{
    [super viewDidAppear:animated];

    self.view.layer.cornerRadius = 10;
    self.view.layer.masksToBounds = YES;
    self.view.superview.backgroundColor = [UIColor clearColor];
}

设置backgroundColor之前:

设置backgroundColor后:

【讨论】:

  • 确认工作 - 也在 iOS 7 中 - 该死,要是我早点找到这个就好了......!
【解决方案2】:

试试

[self.view superview].layer.cornerRadius = 21.0f;
[self.view superview].layer.masksToBounds = YES;

【讨论】:

  • 我在 viewWillAppear 、 viewDidAppear 、 viewWillLayoutSubViews 中尝试过这段代码,没有运气override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.view.superview?.layer.cornerRadius = 0.0 self.view.superview?.layer.masksToBounds = true }
【解决方案3】:

我知道这是一个老问题,但值得回答......

在 iPad 上的模态视图(使用 UIModalPresentationFormSheet)有一个默认背景,它是一个带有白色狗耳朵角的白色边框图像。无论如何,如果您的模态视图背景具有圆角或透明角,则默认背景将显示出来。

我花了相当多的时间试图弄清楚如何在没有默认背景显示的情况下显示我的背景视图。诀窍是使超级视图小于您的视图:

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

注意:vc 是您呈现的视图。不要让视图太小,否则你的触摸事件将不起作用。

【讨论】:

    【解决方案4】:

    这很奇怪。如果可行,也试试self.view.layer.masksToBounds = YES;。不过,您可能会失去视图的阴影。

    想一想,那个白色的东西可能来自导航栏下方的视图。

    【讨论】:

    • 谢谢,eruainon,虽然底角也有白色的角。另外,我尝试使用您提供的代码行,但角落仍然是白色且可见。奇怪。
    【解决方案5】:

    在应用委托中试用:[[self window] setBackgroundColor:[UIColor blackColor]];

    【讨论】:

    • 您可以发布您的视图层次结构的表示吗?
    【解决方案6】:

    我的情况是白色的东西隐藏在图层背景颜色的阴影视图中。 通过将背景更改为清除颜色来解决:

    //Monotouch code
    View.Superview.Layer.BackgroundColor = UIColor.Red.CGColor;
    

    如果没有去掉白角继续搜索,记得查看View、SuperView和BackgroundViews的一些属性:

    • 背景颜色
    • 图层的背景颜色
    • 图层的边框宽度和颜色
    • 标题
    • 剪辑到边界

    注意:DropShadow 在 ViewWillApper 处作为 View 的 SuperView 可见

    【讨论】:

      【解决方案7】:

      我同意@PeterPurple 的回答,但我花了一些时间才想出真正有效的代码。我的示例显示了一个水平居中的模态视图,但更多地位于屏幕顶部,因此视图不会阻碍键盘。当然,我可以在键盘显示时向上移动视图,但我很懒。无论如何,这就是我的做法。

      我创建了一个自定义模态序列,并在该序列中覆盖了 perform: 方法:

      @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);
      }
      

      【讨论】:

        【解决方案8】:

        为了使模态视图透明并在视图出现之前使透明发生,将 self.view.superview.backgroundColor = [UIColor clearColor] 从 viewDidAppear 移动到以下内容:

        • (void)viewWillLayoutSubviews{ [超级视图WillLayoutSubviews];

          self.view.superview.backgroundColor = [UIColor clearColor]; }

        【讨论】:

          【解决方案9】:

          如果您使用 popoverpresentationcontroller 来显示您的视图控制器,请尝试将 popoverpresentationcontroller 的背景颜色设置为清除

              examplePopUpController = exampleViewController.popoverPresentationController!
              examplePopUpController?.backgroundColor = UIColor.clear
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-11-27
            • 2011-12-02
            • 2020-09-03
            相关资源
            最近更新 更多