自定义Uiview的子视图什么时候知道自己的界限?

发布于 2025-01-23 21:52:09 字数 574 浏览 0 评论 0原文

我创建了一个具有一个子视图的自定义Uiview,该子视图包含几个使用布局约束的大小的Square子视图。我希望每个子视图都是圆形的,因此我在数组中持有对视图的引用,然后在layoutsubviews方法中迭代一系列视图,并应用一个角度半径,该转角半径是View.Bounds.height的一半。

中”内部的三个圆形视图

是,这仅有70%的时间。有时,视图似乎不知道其界限,并且视图被视为正方形。

主视图,内容视图,然后在“> 我可能正在接近这一切的 ……有人对我有任何建议吗?在哪里可以安全地施加拐角半径,以便它总是作为一个完美的圆圈?

谢谢

I've created a custom UIView with a subview containing several square subviews which are sized using layout constraints. I'd like for each subview to be circular so I'm holding a reference to the view in an array, then iterating through the array of views in the layoutSubviews method and applying a corner radius that is half the view.bounds.height.

image showing main view, content view, then three circular views inside

The problem is, this only works 70% of the time. Sometimes the view doesn't seem to know its bounds and the views are rendered as square.

image showing main view, content view, then three square views inside

I'm probably approaching this all wrong... does anyone have any advice for me? Where is it safe to apply the corner radius so that it always renders as a perfect circle?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笛声青案梦长安 2025-01-30 21:52:10

管理此操作的最可靠方法是子类uiview,让它处理自己的舍入。

例如:

class RoundView: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        // this assumes constraints keep
        //  self at a 1:1 ratio (square)
        layer.cornerRadius = bounds.height * 0.5
    }
}

此简单控制器示例:

class RoundDemoVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let colors: [UIColor] = [
            .systemRed, .systemGreen, .systemBlue
        ]
        let widths: [CGFloat] = [
            200, 140, 140
        ]
        let xPos: [CGFloat] = [
            20, 120, 180
        ]
        let g = view.safeAreaLayoutGuide
        for i in 0..<colors.count {
            let v = RoundView()
            v.backgroundColor = colors[i]
            view.addSubview(v)
            v.translatesAutoresizingMaskIntoConstraints = false
            v.widthAnchor.constraint(equalToConstant: widths[i]).isActive = true
            // make it square
            v.heightAnchor.constraint(equalTo: v.widthAnchor).isActive = true
            v.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: xPos[i]).isActive = true
            v.centerYAnchor.constraint(equalTo: g.centerYAnchor).isActive = true
        }
        
    }
    
}

生成此输出:

The most reliable way to manage this is to subclass UIView and let it handle its own rounding.

For example:

class RoundView: UIView {
    override func layoutSubviews() {
        super.layoutSubviews()
        // this assumes constraints keep
        //  self at a 1:1 ratio (square)
        layer.cornerRadius = bounds.height * 0.5
    }
}

This simple controller example:

class RoundDemoVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let colors: [UIColor] = [
            .systemRed, .systemGreen, .systemBlue
        ]
        let widths: [CGFloat] = [
            200, 140, 140
        ]
        let xPos: [CGFloat] = [
            20, 120, 180
        ]
        let g = view.safeAreaLayoutGuide
        for i in 0..<colors.count {
            let v = RoundView()
            v.backgroundColor = colors[i]
            view.addSubview(v)
            v.translatesAutoresizingMaskIntoConstraints = false
            v.widthAnchor.constraint(equalToConstant: widths[i]).isActive = true
            // make it square
            v.heightAnchor.constraint(equalTo: v.widthAnchor).isActive = true
            v.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: xPos[i]).isActive = true
            v.centerYAnchor.constraint(equalTo: g.centerYAnchor).isActive = true
        }
        
    }
    
}

Produces this output:

enter image description here

No need to iterate through subviews and setting values based on frame sizes... all you need to do is set the size/position constraints.

黒涩兲箜 2025-01-30 21:52:10

Donmag的示例很酷。此外,您可以这样做,而无需为LayoutsUbview编写代码。您必须在 中添加子视图的约束之后,必须添加此设置

 .......
 ....... // constraint settings

 subView.layoutIfNeeded()
 subView.layer.cornerRadius = subView.frame.size.height/2
    

Donmag's example is cool.Also, you can do it this way without writing code to layoutsubview. You must add this settings after adding the constraints of the subviews in for iteration.

 .......
 ....... // constraint settings

 subView.layoutIfNeeded()
 subView.layer.cornerRadius = subView.frame.size.height/2
    
半透明的墙 2025-01-30 21:52:10

您可以在layoutsubviews()方法中应用角半径

override func layoutSubviews() {
    super.layoutSubviews()
    // This is the place to apply corner radius
}

You can apply the corner radius inside the layoutSubviews() method

override func layoutSubviews() {
    super.layoutSubviews()
    // This is the place to apply corner radius
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文