1
1
import SwiftCrossUI
2
2
import UIKit
3
3
4
- final class ScrollWidget : WrapperWidget < UIScrollView > {
4
+ final class ScrollWidget : ContainerWidget {
5
+ private var scrollView = UIScrollView ( )
5
6
private var childWidthConstraint : NSLayoutConstraint ?
6
7
private var childHeightConstraint : NSLayoutConstraint ?
7
8
8
- private let innerChild : BaseWidget
9
+ private lazy var contentLayoutGuideConstraints : [ NSLayoutConstraint ] = [
10
+ scrollView. contentLayoutGuide. leadingAnchor. constraint ( equalTo: child. view. leadingAnchor) ,
11
+ scrollView. contentLayoutGuide. trailingAnchor. constraint ( equalTo: child. view. trailingAnchor) ,
12
+ scrollView. contentLayoutGuide. topAnchor. constraint ( equalTo: child. view. topAnchor) ,
13
+ scrollView. contentLayoutGuide. bottomAnchor. constraint ( equalTo: child. view. bottomAnchor) ,
14
+ ]
9
15
10
- init ( child innerChild: BaseWidget ) {
11
- self . innerChild = innerChild
12
- super. init ( child: UIScrollView ( ) )
13
-
14
- child. addSubview ( innerChild)
16
+ override func loadView( ) {
17
+ view = scrollView
18
+ scrollView. translatesAutoresizingMaskIntoConstraints = false
19
+ }
15
20
16
- NSLayoutConstraint . activate ( [
17
- innerChild. topAnchor. constraint ( equalTo: child. contentLayoutGuide. topAnchor) ,
18
- innerChild. bottomAnchor. constraint ( equalTo: child. contentLayoutGuide. bottomAnchor) ,
19
- innerChild. leftAnchor. constraint ( equalTo: child. contentLayoutGuide. leftAnchor) ,
20
- innerChild. rightAnchor. constraint ( equalTo: child. contentLayoutGuide. rightAnchor) ,
21
- ] )
21
+ override func updateViewConstraints( ) {
22
+ NSLayoutConstraint . activate ( contentLayoutGuideConstraints)
23
+ super. updateViewConstraints ( )
22
24
}
23
25
24
26
func setScrollBars(
@@ -29,8 +31,8 @@ final class ScrollWidget: WrapperWidget<UIScrollView> {
29
31
case ( true , true ) :
30
32
childHeightConstraint!. isActive = false
31
33
case ( false , nil ) :
32
- childHeightConstraint = innerChild . heightAnchor. constraint (
33
- equalTo: child . heightAnchor)
34
+ childHeightConstraint = child . view . heightAnchor. constraint (
35
+ equalTo: scrollView . heightAnchor)
34
36
fallthrough
35
37
case ( false , false ) :
36
38
childHeightConstraint!. isActive = true
@@ -42,68 +44,68 @@ final class ScrollWidget: WrapperWidget<UIScrollView> {
42
44
case ( true , true ) :
43
45
childWidthConstraint!. isActive = false
44
46
case ( false , nil ) :
45
- childWidthConstraint = innerChild. widthAnchor. constraint ( equalTo: child. widthAnchor)
47
+ childWidthConstraint = child. view. widthAnchor. constraint (
48
+ equalTo: scrollView. widthAnchor)
46
49
fallthrough
47
50
case ( false , false ) :
48
51
childWidthConstraint!. isActive = true
49
52
default :
50
53
break
51
54
}
52
55
53
- child . showsVerticalScrollIndicator = hasVerticalScrollBar
54
- child . showsHorizontalScrollIndicator = hasHorizontalScrollBar
56
+ scrollView . showsVerticalScrollIndicator = hasVerticalScrollBar
57
+ scrollView . showsHorizontalScrollIndicator = hasHorizontalScrollBar
55
58
}
56
59
}
57
60
58
61
extension UIKitBackend {
59
62
public func createContainer( ) -> Widget {
60
- BaseWidget ( )
63
+ BaseViewWidget ( )
61
64
}
62
65
63
66
public func removeAllChildren( of container: Widget ) {
64
- container. subviews . forEach { $0. removeFromSuperview ( ) }
67
+ container. childWidgets . forEach { $0. removeFromParentWidget ( ) }
65
68
}
66
69
67
70
public func addChild( _ child: Widget , to container: Widget ) {
68
- container. addSubview ( child)
71
+ container. add ( childWidget : child)
69
72
}
70
73
71
74
public func setPosition(
72
75
ofChildAt index: Int ,
73
76
in container: Widget ,
74
77
to position: SIMD2 < Int >
75
78
) {
76
- guard index < container. subviews . count else {
79
+ guard index < container. childWidgets . count else {
77
80
assertionFailure ( " Attempting to set position of nonexistent subview " )
78
81
return
79
82
}
80
83
81
- let child = container. subviews [ index] as! BaseWidget
84
+ let child = container. childWidgets [ index]
82
85
child. x = position. x
83
86
child. y = position. y
84
87
}
85
88
86
89
public func removeChild( _ child: Widget , from container: Widget ) {
87
- assert ( child. isDescendant ( of: container) )
88
- child. removeFromSuperview ( )
90
+ assert ( child. view . isDescendant ( of: container. view ) )
91
+ child. removeFromParentWidget ( )
89
92
}
90
93
91
94
public func createColorableRectangle( ) -> Widget {
92
- BaseWidget ( )
95
+ BaseViewWidget ( )
93
96
}
94
97
95
98
public func setColor( ofColorableRectangle widget: Widget , to color: Color ) {
96
- widget. backgroundColor = color. uiColor
99
+ widget. view . backgroundColor = color. uiColor
97
100
}
98
101
99
102
public func setCornerRadius( of widget: Widget , to radius: Int ) {
100
- widget. layer. cornerRadius = CGFloat ( radius)
101
- widget. layer. masksToBounds = true
102
- widget. setNeedsLayout ( )
103
+ widget. view. layer. cornerRadius = CGFloat ( radius)
104
+ widget. view. layer. masksToBounds = true
103
105
}
104
106
105
107
public func naturalSize( of widget: Widget ) -> SIMD2 < Int > {
106
- let size = widget. intrinsicContentSize
108
+ let size = widget. view . intrinsicContentSize
107
109
return SIMD2 (
108
110
Int ( size. width. rounded ( . awayFromZero) ) ,
109
111
Int ( size. height. rounded ( . awayFromZero) )
0 commit comments