From 8fa2d847b6070f13f5a18e63c7d82cf56530a8b5 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Fri, 15 Jun 2018 11:25:14 -0700 Subject: [PATCH] Fabric: `-[RCTViewComponentView setContentView:]`: Summary: This is just the convenient way to embed native views inside custom View components. See coming implementation as an example. Reviewed By: fkgozali Differential Revision: D8344056 fbshipit-source-id: 7f5f8cfeeffa7676bc7b562aa07f006cb9006575 --- .../View/RCTViewComponentView.h | 10 ++++++++ .../View/RCTViewComponentView.mm | 25 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h index 73c3302a6..d45bcce07 100644 --- a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h +++ b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.h @@ -26,6 +26,16 @@ NS_ASSUME_NONNULL_BEGIN facebook::react::SharedViewEventEmitter _eventEmitter; } +/** + * Represents the `UIView` instance that is being automatically attached to + * the component view and laid out using on `layoutMetrics` (especially `size` + * and `padding`) of the component. + * This view must not be a component view; it's just a convenient way + * to embed/bridge pure native views as component views. + * Defaults to `nil`. Assing `nil` to remove view as subview. + */ +@property (nonatomic, strong, nullable) UIView *contentView; + @end NS_ASSUME_NONNULL_END diff --git a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index f6e74eda4..a5f0c6728 100644 --- a/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -14,6 +14,28 @@ using namespace facebook::react; @implementation RCTViewComponentView +- (void)setContentView:(UIView *)contentView +{ + if (_contentView) { + [_contentView removeFromSuperview]; + } + + _contentView = contentView; + + if (_contentView) { + [self addSubview:_contentView]; + } +} + +- (void)layoutSubviews +{ + [super layoutSubviews]; + + if (_contentView) { + _contentView.frame = RCTCGRectFromRect(_layoutMetrics.getContentFrame()); + } +} + - (void)updateProps:(SharedProps)props oldProps:(SharedProps)oldProps @@ -42,9 +64,10 @@ using namespace facebook::react; - (void)updateLayoutMetrics:(LayoutMetrics)layoutMetrics oldLayoutMetrics:(LayoutMetrics)oldLayoutMetrics { + _layoutMetrics = layoutMetrics; + [super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics]; - _layoutMetrics = layoutMetrics; } #pragma mark - Accessibility Events