From da9e839049bcdf66b4ac4ca18f65d3db4fe76861 Mon Sep 17 00:00:00 2001 From: Domas Nutautas Date: Fri, 15 May 2020 23:58:19 +0300 Subject: [PATCH] Copy constraints --- Sources/NibLoader.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/NibLoader.swift b/Sources/NibLoader.swift index 2738c7f..8d08d05 100644 --- a/Sources/NibLoader.swift +++ b/Sources/NibLoader.swift @@ -21,6 +21,7 @@ public struct IBNibLoader where NibLoadableView: U let nibView = type(of: view).fromNib() copyProperties(to: nibView) + copyConstraints(to: nibView) return nibView } @@ -71,6 +72,20 @@ public struct IBNibLoader where NibLoadableView: U nibView.isHidden = view.isHidden } + private func copyConstraints(to nibView: UIView) { + nibView.addConstraints( + view.constraints.map { + NSLayoutConstraint( + item: $0.firstItem === view ? nibView : $0.firstItem as Any, + attribute: $0.firstAttribute, + relatedBy: $0.relation, + toItem: $0.secondItem === view ? nibView : $0.secondItem, + attribute: $0.secondAttribute, + multiplier: $0.multiplier, + constant: $0.constant) + } + ) + } }