@@ -29,38 +29,70 @@ private protocol WidgetProtocolHelpers: WidgetProtocol {
2929
3030extension WidgetProtocolHelpers {
3131 func updateLeftConstraint( ) {
32- leftConstraint? . isActive = false
33- guard let superview = view. superview else { return }
34- leftConstraint = view. leftAnchor. constraint (
35- equalTo: superview. safeAreaLayoutGuide. leftAnchor, constant: CGFloat ( x) )
36- // Set the constraint priority for leftConstraint (and topConstraint) to just under
37- // "required" so that we don't get warnings about unsatisfiable constraints from
38- // scroll views, which position relative to their contentLayoutGuide instead.
39- // This *should* be high enough that it won't cause any problems unless there was
40- // a constraint conflict anyways.
41- leftConstraint!. priority = . init( UILayoutPriority . required. rawValue - 1.0 )
42- leftConstraint!. isActive = true
32+ guard let superview = view. superview else {
33+ leftConstraint? . isActive = false
34+ return
35+ }
36+
37+ if let leftConstraint,
38+ leftConstraint. secondAnchor === superview. safeAreaLayoutGuide. leftAnchor
39+ {
40+ leftConstraint. constant = CGFloat ( x)
41+ leftConstraint. isActive = true
42+ } else {
43+ self . leftConstraint? . isActive = false
44+ let leftConstraint = view. leftAnchor. constraint (
45+ equalTo: superview. safeAreaLayoutGuide. leftAnchor, constant: CGFloat ( x) )
46+ self . leftConstraint = leftConstraint
47+ // Set the constraint priority for leftConstraint (and topConstraint) to just
48+ // under "required" so that we don't get warnings about unsatisfiable constraints
49+ // from scroll views, which position relative to their contentLayoutGuide instead.
50+ // This *should* be high enough that it won't cause any problems unless there was
51+ // a constraint conflict anyways.
52+ leftConstraint. priority = . init( UILayoutPriority . required. rawValue - 1.0 )
53+ leftConstraint. isActive = true
54+ }
4355 }
4456
4557 func updateTopConstraint( ) {
46- topConstraint? . isActive = false
47- guard let superview = view. superview else { return }
48- topConstraint = view. topAnchor. constraint (
49- equalTo: superview. safeAreaLayoutGuide. topAnchor, constant: CGFloat ( y) )
50- topConstraint!. priority = . init( UILayoutPriority . required. rawValue - 1.0 )
51- topConstraint!. isActive = true
58+ guard let superview = view. superview else {
59+ topConstraint? . isActive = false
60+ return
61+ }
62+
63+ if let topConstraint,
64+ topConstraint. secondAnchor === superview. safeAreaLayoutGuide. topAnchor
65+ {
66+ topConstraint. constant = CGFloat ( y)
67+ topConstraint. isActive = true
68+ } else {
69+ self . topConstraint? . isActive = false
70+ let topConstraint = view. topAnchor. constraint (
71+ equalTo: superview. safeAreaLayoutGuide. topAnchor, constant: CGFloat ( y) )
72+ self . topConstraint = topConstraint
73+ topConstraint. priority = . init( UILayoutPriority . required. rawValue - 1.0 )
74+ topConstraint. isActive = true
75+ }
5276 }
5377
5478 func updateWidthConstraint( ) {
55- widthConstraint? . isActive = false
56- widthConstraint = view. widthAnchor. constraint ( equalToConstant: CGFloat ( width) )
57- widthConstraint!. isActive = true
79+ if let widthConstraint {
80+ widthConstraint. constant = CGFloat ( width)
81+ } else {
82+ let widthConstraint = view. widthAnchor. constraint ( equalToConstant: CGFloat ( width) )
83+ self . widthConstraint = widthConstraint
84+ widthConstraint. isActive = true
85+ }
5886 }
5987
6088 func updateHeightConstraint( ) {
61- heightConstraint? . isActive = false
62- heightConstraint = view. heightAnchor. constraint ( equalToConstant: CGFloat ( height) )
63- heightConstraint!. isActive = true
89+ if let heightConstraint {
90+ heightConstraint. constant = CGFloat ( height)
91+ } else {
92+ let heightConstraint = view. heightAnchor. constraint ( equalToConstant: CGFloat ( height) )
93+ self . heightConstraint = heightConstraint
94+ heightConstraint. isActive = true
95+ }
6496 }
6597}
6698
0 commit comments