0

我正在尝试为 UIView 编写扩展,以便更轻松地为视图设置锚点。

这个想法是写一个这样的 setAnchors 方法:

import UIKit

extension UIView {

    func setAnchors(top: Anchor? = nil,
                    bottom: Anchor? = nil,
                    leading: Anchor? = nil,
                    trailing: Anchor? = nil) {
        translatesAutoresizingMaskIntoConstraints = false
        if let top = top, let anchorType = top.type as? NSLayoutAnchor<NSLayoutYAxisAnchor>, let constant = top.constant {
            let constraint = topAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }

        if let bottom = bottom, let anchorType = bottom.type as? NSLayoutAnchor<NSLayoutYAxisAnchor>, let constant = bottom.constant {
            let constraint = bottomAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }

        if let leading = leading, let anchorType = leading.type as? NSLayoutAnchor<NSLayoutXAxisAnchor>, let constant = leading.constant {
            let constraint = leadingAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }

        if let trailing = trailing, let anchorType = trailing.type as? NSLayoutAnchor<NSLayoutXAxisAnchor>, let constant = trailing.constant {
            let constraint = trailingAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }
    }
}

struct Anchor {
    var type: NSLayoutAnchor<AnyObject>
    var constant: CGFloat? = 0.0
}

可以这样调用:

 topView.setAnchors(top: Anchor(type: view.topAnchor), leading: Anchor(type: view.leadingAnchor), trailing: Anchor(type: view.trailingAnchor))

我收到以下错误:

无法将“NSLayoutAnchor”类型的值转换为预期的参数类型“NSLayoutAnchor”

在此处输入图像描述

我知道我可以给 topAnchor asNSLayoutYAxisAnchor等等,并给这个方法提供常量作为参数来使它工作,但我想知道是否有办法让它与这个 Anchor 结构一起工作?

4

1 回答 1

0

您可以使用通用。

struct Anchor<T: AnyObject>{
    var type: NSLayoutAnchor<T>
    var constant: CGFloat? = 0.0
}

extension UIView {
    func setAnchors<T: AnyObject>(top: Anchor<T>? = nil,
                    bottom: Anchor<T>? = nil,
                    leading: Anchor<T>? = nil,
                    trailing: Anchor<T>? = nil) {
        translatesAutoresizingMaskIntoConstraints = false
        if let top = top, let anchorType = top.type as? NSLayoutAnchor<NSLayoutYAxisAnchor>, let constant = top.constant {
            let constraint = topAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }

        if let bottom = bottom, let anchorType = bottom.type as? NSLayoutAnchor<NSLayoutYAxisAnchor>, let constant = bottom.constant {
            let constraint = bottomAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }

        if let leading = leading, let anchorType = leading.type as? NSLayoutAnchor<NSLayoutXAxisAnchor>, let constant = leading.constant {
            let constraint = leadingAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }

        if let trailing = trailing, let anchorType = trailing.type as? NSLayoutAnchor<NSLayoutXAxisAnchor>, let constant = trailing.constant {
            let constraint = trailingAnchor.constraint(equalTo: anchorType, constant: constant)
            constraint.isActive = true
        }
    }
}
于 2021-01-25T17:05:09.597 回答