uiview - 圆形 UIView 不是完整的圆形

标签 uiview interface-builder cornerradius ibinspectable

我有一个有趣的问题,但我不确定如何解决它。我正在尝试使用 @IBInspectable 扩展 UIView。但是,使用这种方法,拐角半径似乎是从 nib 文件的默认端设置的,而不是 View 的实际大小。

因此,当我在 IB 中将“View as”设置为 iPhoneSE 并为 iPhoneSE 构建时, View 是一个圆圈。但是,如果我为 iPhone7 构建,角没有完全圆成一个圆。相反,如果我将“View as”设置为 iPhone7 并为 iPhone7 构建,则 View 是一个圆,但是,如果我为 iPhoneSE 构建,则边角会过圆。

图片及代码如下:

扩展

extension UIView {

    @IBInspectable var cornerRadius:Double {
        get {
            return Double(layer.cornerRadius)
        }
        set {
            layer.cornerRadius = CGFloat(newValue)
            layer.masksToBounds = newValue > 0
        }
    }

    @IBInspectable var circleView:Bool {
        get {
            return layer.cornerRadius == min(self.frame.width, self.frame.height) / CGFloat(2.0) ? true : false
        }
        set {
            if newValue {
                layer.cornerRadius = min(self.frame.width, self.frame.height) / CGFloat(2.0)
                layer.masksToBounds = true
            }
            else{
                layer.cornerRadius = 0.0
                layer.masksToBounds = false
            }
        }
    }

}

在IB中“View as”设置为iPhoneSE

专为 iPhoneSE 打造

enter image description here

为 iPhone 7 构建

enter image description here

“查看为”设置为iPhone7

为 iPhone SE 打造

enter image description here

为 iPhone 7 构建

enter image description here

IB 设置 enter image description here enter image description here

最佳答案

如果您的 View 是正方形,您只会得到一个完整的圆圈。

如果您的 View 是一个矩形,并且您将 cornerRadius 的值设置为小于最小尺寸的一半,您将获得第二个 View ,如果它的值大于该值,您将获得菱形的。

检查您的约束,您应该在 View 完成 viewDidLayout 中的布局后计算 cornerRadius,以便获得正确的尺寸。

这是一个展示这个的 Playground (我添加了一个动画来更好地展示这个问题):

import UIKit
import PlaygroundSupport

extension UIView
{
  func addCornerRadiusAnimation(from: CGFloat, to: CGFloat, duration: CFTimeInterval)
  {
    let animation = CABasicAnimation(keyPath:"cornerRadius")
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
    animation.fromValue = from
    animation.toValue = to
    animation.duration = duration
    self.layer.add(animation, forKey: "cornerRadius")
    self.layer.cornerRadius = to
  }
}

let v = UIView(frame: CGRect(x: 0, y: 0, width: 350, height: 450))
PlaygroundPage.current.liveView = v

let squareView = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
squareView.backgroundColor = .red
squareView.layer.masksToBounds = true
v.addSubview(squareView)
var cornerRadius = CGFloat(0.5*min(squareView.frame.width,
                                   squareView.frame.height))
squareView.addCornerRadiusAnimation(from: 0, to: cornerRadius, duration: 5)

let rectangleView = UIView(frame: CGRect(x: 10, y: 200, width: 100, height: 200))
rectangleView.backgroundColor = .blue
rectangleView.layer.masksToBounds = true
v.addSubview(rectangleView)
cornerRadius = CGFloat(0.5*min(rectangleView.frame.width,
                                   rectangleView.frame.height))
rectangleView.addCornerRadiusAnimation(from: 0, to: cornerRadius, duration: 5)

let diamondView = UIView(frame: CGRect(x: 200, y: 200, width: 100, height: 200))
diamondView.backgroundColor = .green
diamondView.layer.masksToBounds = true
v.addSubview(diamondView)
cornerRadius = CGFloat(0.5*max(diamondView.frame.width,
                               diamondView.frame.height))
diamondView.addCornerRadiusAnimation(from: 0, to: cornerRadius, duration: 5)

Playground output

关于uiview - 圆形 UIView 不是完整的圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42590005/

相关文章:

ios - 如何在 Swift 中制作圆形 UIView

ios - 在设备方向改变时交叉淡入淡出某些 UIView

ios - JASidePanels,设备是横向的,但面板仍然是纵向的

ipad - 让 Interface Builder 在 SplitViewControllers DetailView 中布局我的内容

swift - UIVisualEffectView 上的圆角半径

ios - 如何制作uiimageview圆。?

ios - 在 iOS 中,将 View 添加为 subview 与将 View 分配给 View 属性之间有区别吗?

ios - 使用 UIGesturerecognizer 或其他方式移动时如何知道 subview 是否相交?

ios - 如何仅在 Interface Builder 中使用键盘移动控件

ios - 在 UILabel 之后放置一个 UIImage