ios - 尝试在 UIView 上圆角时出现奇怪的行为

标签 ios swift uiview border

我现在在 Swift3 中的 UIView 中与奇怪的行为作斗争了几个小时。我只是想添加一个位于屏幕中心的 UIView,带有彩色和圆形边框。

为了轻松地圆化边框,我使用以下扩展:

extension UIView {

 /**
 Rounds the given set of corners to the specified radius

 - parameter corners: Corners to round
 - parameter radius:  Radius to round to
 */
 func round(corners: UIRectCorner, radius: CGFloat) {
    _round(corners: corners, radius: radius)
 }

 /**
 Rounds the given set of corners to the specified radius with a border

 - parameter corners:     Corners to round
 - parameter radius:      Radius to round to
 - parameter borderColor: The border color
 - parameter borderWidth: The border width
 */
 func round(corners: UIRectCorner, radius: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
    let mask = _round(corners: corners, radius: radius)
    addBorder(mask: mask, borderColor: borderColor, borderWidth: borderWidth)
 }

/**
 Fully rounds an autolayout view (e.g. one with no known frame) with the given diameter and border

 - parameter diameter:    The view's diameter
 - parameter borderColor: The border color
 - parameter borderWidth: The border width
 */
 func fullyRound(diameter: CGFloat, borderColor: UIColor, borderWidth: CGFloat) {
    layer.masksToBounds = true
    layer.cornerRadius = diameter / 2
    layer.borderWidth = borderWidth
    layer.borderColor = borderColor.cgColor;
 }

}

private extension UIView {

 @discardableResult func _round(corners: UIRectCorner, radius: CGFloat)   -> CAShapeLayer {
    let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
    let mask = CAShapeLayer()
    mask.path = path.cgPath
    self.layer.mask = mask
    return mask
 }

 func addBorder(mask: CAShapeLayer, borderColor: UIColor, borderWidth: CGFloat) {
    let borderLayer = CAShapeLayer()
    borderLayer.path = mask.path
    borderLayer.fillColor = UIColor.clear.cgColor
    borderLayer.strokeColor = borderColor.cgColor
    borderLayer.lineWidth = borderWidth
    borderLayer.frame = bounds
    layer.addSublayer(borderLayer)
 }
}

class GameViewController: UIViewController {
 @IBOutlet weak var cardView: UIView!

 override func viewDidLoad() {
    super.viewDidLoad()

    cardView.round(corners: UIRectCorner.allCorners, radius: 5.0, borderColor: DesignHelper.defaultColor, borderWidth: 1)
    }
 }

我只是使用正确的参数调用 myView.round(),但它没有按预期工作。

  1. 首先,当我只是在 Storyboard中添加 UIView 时,而不尝试添加边框和颜色,我的行为是正确的。换句话说, my UIView is correctly centered inside the screen

  2. 但是,一旦我尝试使用 UIView 扩展并添加彩色边框, the view is not correctly centered anymore 。右边的空间好像被什么东西剪掉了。

我的猜测是用于绘制边框的蒙版/图层的边界或框架存在问题。但我没有找到任何可以纠正这一点的东西。

有什么建议吗?

最佳答案

实际上,我是在 UIViewController 的 viewDidLoad 中调用 round 方法。然而,正如 @luk2302 所注意到的,这不是处理边界、框架等的正确位置,因为此时 View 尚未完全加载。

因此,我将调用移至 UIViewController 的 viewDidLayoutSubviews 方法内的 round 方法。工作得很好!

关于ios - 尝试在 UIView 上圆角时出现奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41370794/

相关文章:

ios - 我想将 CIImage 转换为 UIImage 但失败了

ios - 带有GL_DEPTH_COMPONENT的glTexImage2D在iOS4上可运行,在iOS 5上无法运行GL_INVALID_ENUM

ios - Xcode 用户界面测试 : Executing a Test Conditionally

ios - 更改 UIImage 中的白色像素

iOS -如何让单独的 UIView 控制 UISlider 的拇指矩形

iphone - 我无法在 UIView 类之外初始化 CALayer 是否有原因?

ios - 'MyClass' 不可用 : cannot find Swift declaration for this class - Simulator

ios - 使用iOS版Gradle上传HockeyApp版本

iOS:如何手动将 2D Float 数据设置为 MTLTexture 或 MPSImage?

ios - 当键盘弹出时将原点移动到顶部时会出现黑色