1
1
import UIKit
2
- import Cartography
3
2
4
3
public final class Content : NSObject {
5
4
public var view : UIView
@@ -12,7 +11,7 @@ public final class Content: NSObject {
12
11
}
13
12
14
13
public private( set) var initialPosition : Position
15
- private let group = ConstraintGroup ( )
14
+ private var constraints = [ NSLayoutConstraint ] ( )
16
15
17
16
public init ( view: UIView , position: Position , centered: Bool = true ) {
18
17
self . view = view
@@ -22,40 +21,70 @@ public final class Content: NSObject {
22
21
23
22
super. init ( )
24
23
25
- constrain ( view) { [ unowned self] view in
26
- view. width == self . view. frame. width
27
- view. height == self . view. frame. height
28
- }
24
+ view. translatesAutoresizingMaskIntoConstraints = false
25
+ setupSizeConstraints ( )
29
26
}
30
27
31
28
public func layout( ) {
32
- guard view . superview != nil else {
29
+ guard let superview = view . superview else {
33
30
return
34
31
}
35
32
36
- constrain ( view, replace: group) { [ unowned self] view in
37
- let x = self . position. left == 0.0
38
- ? view. superview!. left * 1.0
39
- : view. superview!. right * self . position. left
40
- let y = self . position. top == 0.0
41
- ? view. superview!. top * 1.0
42
- : view. superview!. bottom * self . position. top
43
-
44
- if self . centered {
45
- view. centerX == x
46
- view. centerY == y
47
- } else {
48
- view. left == x
49
- view. top == y
50
- }
51
- }
52
-
33
+ NSLayoutConstraint . deactivate ( constraints)
34
+
35
+ let xAttribute : NSLayoutAttribute = centered ? . centerX : . leading
36
+ let yAttribute : NSLayoutAttribute = centered ? . centerY : . top
37
+ let xSuperAttribute : NSLayoutAttribute = position. left == 0 ? . leading : . trailing
38
+ let ySuperAttribute : NSLayoutAttribute = position. top == 0 ? . top : . bottom
39
+ let xMultiplier : CGFloat = position. left == 0 ? 1 : position. left
40
+ let yMultiplier : CGFloat = position. top == 0 ? 1 : position. top
41
+
42
+ constraints = [
43
+ NSLayoutConstraint (
44
+ item: view,
45
+ attribute: xAttribute,
46
+ relatedBy: . equal,
47
+ toItem: superview,
48
+ attribute: xSuperAttribute,
49
+ multiplier: xMultiplier,
50
+ constant: 0
51
+ ) ,
52
+ NSLayoutConstraint (
53
+ item: view,
54
+ attribute: yAttribute,
55
+ relatedBy: . equal,
56
+ toItem: superview,
57
+ attribute: ySuperAttribute,
58
+ multiplier: yMultiplier,
59
+ constant: 0
60
+ )
61
+ ]
62
+
63
+ NSLayoutConstraint . activate ( constraints)
53
64
view. layoutIfNeeded ( )
54
65
}
55
66
56
67
public func animate( ) {
57
68
view. superview!. layoutIfNeeded ( )
58
69
}
70
+
71
+ private func setupSizeConstraints( ) {
72
+ makeSizeConstraint ( attribute: . width, constant: view. frame. width) . isActive = true
73
+ makeSizeConstraint ( attribute: . height, constant: view. frame. height) . isActive = true
74
+ }
75
+
76
+ private func makeSizeConstraint( attribute: NSLayoutAttribute ,
77
+ constant: CGFloat ) -> NSLayoutConstraint {
78
+ return NSLayoutConstraint (
79
+ item: view,
80
+ attribute: attribute,
81
+ relatedBy: . equal,
82
+ toItem: nil ,
83
+ attribute: . notAnAttribute,
84
+ multiplier: 1 ,
85
+ constant: constant
86
+ )
87
+ }
59
88
}
60
89
61
90
public extension Content {
0 commit comments