斯威夫特需要能够确定类型在编译时间,但你试图返回根据 传递edge
参数要么 NSLayoutAnchor<NSLayoutXAxisAnchor>
或NSLayoutAnchor<NSLayoutYAxisAnchor>
对象。
什么你能要做的就是在您的边缘分成与x轴和y轴的边缘:
extension UIView
{
public enum XLayoutEdge {
case right
// ...
}
public enum YLayoutEdge {
case top
// ...
}
func anchor(for edge: XLayoutEdge) -> NSLayoutAnchor<NSLayoutXAxisAnchor> {
switch edge
{
case .right: return rightAnchor
// ...
}
}
func anchor(for edge: YLayoutEdge) -> NSLayoutAnchor<NSLayoutYAxisAnchor> {
switch edge
{
case .top: return topAnchor
// ...
}
}
public func constrain(edge edge1: XLayoutEdge, to edge2: XLayoutEdge, of view: UIView) -> NSLayoutConstraint {
return anchor(for: edge1).constraint(equalTo: view.anchor(for: edge2))
}
public func constrain(edge edge1: YLayoutEdge, to edge2: YLayoutEdge, of view: UIView) -> NSLayoutConstraint {
return anchor(for: edge1).constraint(equalTo: view.anchor(for: edge2))
}
func useEdges(view: UIView)
{
_ = constrain(edge: .right, to: .right, of: view)
_ = constrain(edge: .top, to: .top, of: view)
}
}
它会变得更糟,因为你将不得不考虑NSLayoutDimension
了。你可以玩泛型 ,但你可能最终会以某种方式复制Apple已经为你设置的地方:)。
这就是为什么我认为你在这里对付系统。退后一步,为什么不直接使用锚?
extension UIView
{
func useAnchors(view: UIView)
{
_ = rightAnchor.constraint(equalTo: view.rightAnchor)
_ = topAnchor.constraint(equalTo: view.bottomAnchor)
}
}
如果你想写自己方便的功能,你可以做这样的:
extension UIView
{
public func constrain<T>(_ anchor1: NSLayoutAnchor<T>, to anchor2: NSLayoutAnchor<T>) -> NSLayoutConstraint {
return anchor1.constraint(equalTo: anchor2)
}
func useYourOwnFunctions(view: UIView)
{
_ = constrain(rightAnchor, to: view.rightAnchor)
_ = constrain(topAnchor, to: view.bottomAnchor)
}
}
来源
2017-05-19 09:25:16
thm