iOS10 - 圆角半径不适用于边框

iOS10 - Corner Radius is not working right with border

我有一个 UIViewController,我将其显示为 UIModalPresentationPopover。它的角半径设置为 10.0,边框宽度设置为 1.0。

- (void)viewWillLayoutSubviews {
  [super viewWillLayoutSubviews];     

  self.view.layer.borderWidth = 1.0f;
  self.view.layer.cornerRadius = 10.0f;
  self.view.layer.masksToBounds = YES;
}

它没有显示沿角半径的任何边界,并且产生了一种奇怪的效果。它在 iOS10 之前工作正常。我应该怎么做才能解决这个问题?

编辑:截图如下

如果我添加 2 个像素边框,那么视图中仍然缺少 1 个像素

你的代码很好,但是少了一行,

view.clipsToBounds = true

设置此行并再次运行。

希望对您有所帮助。

新一件事变成XCode8我们不能直接设置layercornerRadius.

当你想应用cornerRadiusUIView时需要在应用cornerRadius之前添加一行代码。

yourButton.layoutIfNeeded()

例子变成Objective C.

[yourButton layoutIfNeeded];
yourButton.layer.cornerRadius = yourButton.frame.size.height/2;
[[yourButton layer] setBorderWidth:2.0f];

Swift3 示例

self.layoutIfNeeded()
yourButton.layer.cornerRadius = self.frame.height / 2.0
yourButton.layer.borderWidth = 2.0

我通过以下代码解决了我的问题

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")){
    self.view.superview.layer.cornerRadius = 15.0f;
}

我确实将视图角半径设置为 10.0 所以 15.0superview 不会干预其 subview 角半径和边框以将其显示为模糊或少 1 个像素。

编辑:自定义创建的 UIViewController 如果动画可能需要在完成动画后实现上面的代码。