简体   繁体   English

SWIFT - 将 layer.cornerRadius 设置为大于视图高度会导致问题

[英]SWIFT - Setting layer.cornerRadius greater than the view height causes issues

I actually want to add a corner radius to a view on only one side.我实际上只想在一侧的视图中添加角半径。 The radius is supposed to be of full height.半径应该是全高。 This is my implementation.这是我的实现。

someView.layer.cornerRadius = someView.frame.size.height
someView.layer.maskedCorners = [.layerMaxXMaxYCorner]

This does the job as required, but it adds extra shapes on other sides as shown below.这会按要求完成工作,但它会在其他侧面添加额外的形状,如下所示。

What might be the problem?可能是什么问题? 在此处输入图像描述

This looks like an iOS bug, I was able to reproduce it这看起来像一个 iOS 错误,我能够重现它

But usually you don't wanna set cornerRadius greater than half view minimum side, in your case:但通常你不想设置cornerRadius大于半视图最小边,在你的情况下:

someView.layer.cornerRadius = someView.frame.size.height / 2

I assume it'll produce result you're expecting:我假设它会产生您期望的结果:

在此处输入图像描述

Corner radius is radius of a circle inscribed in the corner of a rectangle, I think that's why there may be problems with radius bigger than side/2: circle doesn't fit a rectangle anymore角半径是刻在矩形角上的圆的半径,我认为这就是为什么半径大于 side/2 可能会出现问题:圆不再适合矩形

i had the same problem.我有同样的问题。 I needed to make the height of the view 16 and the bottom corners radius also 16.我需要使视图的高度为 16,底角半径也为 16。

My solution:我的解决方案:

  1. add the view you want to round to the bottom view将要舍入的视图添加到底部视图
  2. clip the bottomView to specific size (in my case width = window width and height=16)将 bottomView 剪辑到特定大小(在我的例子中宽度 = window 宽度和高度 = 16)
  3. make height of the rounded view = cornerRadius * 2 (in my case 32)使圆形视图的高度 = cornerRadius * 2(在我的例子中是 32)
  4. pin rounded view to superView, excluding the opposite edge which you want to round off (in my case i wanted to round bottom edge roundedView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top) )将圆形视图固定到 superView,不包括要四舍五入的相对边缘(在我的例子中,我想将底部边缘roundedView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)

code代码

private let bottomView: UIView = {
    let view = UIView(forAutoLayout: ())
    view.clipsToBounds = true

    return view
}()
private let roundedView: UIView = {
    let view = UIView(forAutoLayout: ())
    view.clipsToBounds = true
    view.contentMode = .scaleAspectFill
    view.layer.maskedCorners = [.layerMinXMaxYCorner, .layerMaxXMaxYCorner]

    return view
}()

...
    contentView.addSubview(bottomView)
    bottomView.addSubview(roundedView)

...

func setupConstraints() {
        if roundedView.layer.cornerRadius >= roundedView.frame.height / 2 {
            roundedView.autoSetDimension(.height, toSize: roundedView.layer.cornerRadius * 2)
        } else {
            roundedView.autoPinEdge(toSuperviewEdge: .top)
        }

        roundedView.autoPinEdgesToSuperviewEdges(with: .zero, excludingEdge: .top)
        bottomView.autoPinEdgesToSuperviewEdges()
    }

result:结果:

enter image description here在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 layer.cornerRadius与NSLayoutConstraints(swift 3)不兼容 - layer.cornerRadius not working in conjunction with NSLayoutConstraints (swift 3) UIlabel layer.cornerRadius 在 iOS 7.1 中不起作用 - UIlabel layer.cornerRadius not working in iOS 7.1 较小尺寸的按钮不具有layer.cornerRadius圆形 - Button at smaller size not round with layer.cornerRadius UIView Animation 完成后移除 layer.cornerRadius - UIView Animation Removes layer.cornerRadius After Completion UIButton的layer.cornerRadius删除按钮标题的可见性 - UIButton's layer.cornerRadius removes visibility of button title UIView的子类,不能设置它自己的layer.cornerRadius? - Subclass of UIView, can't set it's own layer.cornerRadius? IOS 7.0:如何更改导航按钮的layer.cornerRadius的值 - IOS 7.0 : How to change values of layer.cornerRadius of a Navigation Button iOS如何删除细边框线然后使用layer.cornerRadius - iOS how to remove thin border line then use layer.cornerRadius 为嵌入到堆栈视图中的视图设置图层的cornerRadius会产生意外结果 - Setting cornerRadius of layer for a view embedeed in stack view gives unexpected results view.Layer.CornerRadius 不适用于 UIView Swift 3 iOS 的 UIScrollView 子视图 - view.Layer.CornerRadius not working on UIScrollView subView of UIView Swift 3 iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM