RCTWrapperView.m
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#import "RCTWrapperView.h"
#import <React/RCTBridge.h>
#import <React/RCTUIManager.h>
@implementation RCTWrapperView {
__weak RCTBridge *_bridge;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
{
if (self = [super initWithFrame:CGRectZero]) {
_bridge = bridge;
__weak __typeof(self) weakSelf = self;
_measureBlock = ^(CGSize minimumSize, CGSize maximumSize) {
__typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return maximumSize;
}
CGSize size = [strongSelf sizeThatFits:maximumSize];
return CGSizeMake(
MAX(size.width, minimumSize.width),
MAX(size.height, minimumSize.height)
);
};
}
return self;
}
#pragma mark - `contentView`
- (nullable UIView *)contentView
{
return self.subviews.firstObject;
}
- (void)setContentView:(UIView *)contentView
{
while (self.subviews.firstObject) {
[self.subviews.firstObject removeFromSuperview];
}
if (!contentView) {
return;
}
[super addSubview:contentView];
contentView.frame = self.bounds;
contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
contentView.translatesAutoresizingMaskIntoConstraints = YES;
}
#pragma mark - Layout
- (void)setNeedsLayout
{
[super setNeedsLayout];
[self invalidateIntrinsicContentSize];
}
- (void)invalidateIntrinsicContentSize
{
[super invalidateIntrinsicContentSize];
// Setting `intrinsicContentSize` dirties the Yoga node and
// enfoce Yoga to call `measure` function (backed to `measureBlock`).
[_bridge.uiManager setIntrinsicContentSize:self.intrinsicContentSize forView:self];
}
- (CGSize)intrinsicContentSize
{
return [self sizeThatFits:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)];
}
- (CGSize)sizeThatFits:(CGSize)size
{
UIView *contentView = self.contentView;
if (!contentView) {
return [super sizeThatFits:size];
}
return [contentView sizeThatFits:size];
}
@end