gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:48:02 26 4
gpt4 key购买 nike

我现在在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com