'RCTRootView's size flexibility' UIExplorer demo

Reviewed By: javache

Differential Revision: D2622491

fb-gh-sync-id: de79a96ee0fef432bbd0512eba8994719a0adad3
This commit is contained in:
Pawel Sienkowski
2015-11-12 11:40:02 -08:00
committed by facebook-github-bot-2
parent f1712b0cc3
commit d5209a0829
6 changed files with 220 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <UIKit/UIKit.h>
#import "RCTView.h"
@interface FlexibleSizeExampleView : RCTView
@end

View File

@@ -0,0 +1,119 @@
/**
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import "AppDelegate.h"
#import "FlexibleSizeExampleView.h"
#import "RCTJavaScriptLoader.h"
#import "RCTBridge.h"
#import "RCTRootView.h"
#import "RCTRootViewDelegate.h"
#import "RCTViewManager.h"
@interface FlexibleSizeExampleViewManager : RCTViewManager
@end
@implementation FlexibleSizeExampleViewManager
RCT_EXPORT_MODULE();
- (UIView *)view
{
return [FlexibleSizeExampleView new];
}
@end
@interface FlexibleSizeExampleView ()<RCTRootViewDelegate>
@end
@implementation FlexibleSizeExampleView
{
RCTRootView *_resizableRootView;
UITextView *_currentSizeTextView;
BOOL _sizeUpdated;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_sizeUpdated = NO;
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
_resizableRootView = [[RCTRootView alloc] initWithBridge:appDelegate.bridge
moduleName:@"RootViewSizeFlexibilityExampleApp"
initialProperties:@{}];
[_resizableRootView setSizeFlexibility:RCTRootViewSizeFlexibilityHeight];
_currentSizeTextView = [UITextView new];
_currentSizeTextView.editable = NO;
_currentSizeTextView.text = @"Resizable view has not been resized yet";
_currentSizeTextView.textColor = [UIColor blackColor];
_currentSizeTextView.backgroundColor = [UIColor whiteColor];
_currentSizeTextView.font = [UIFont boldSystemFontOfSize:10];
_resizableRootView.delegate = self;
[self addSubview:_currentSizeTextView];
[self addSubview:_resizableRootView];
}
return self;
}
- (void)layoutSubviews
{
float textViewHeight = 60;
float spacingHeight = 10;
[_resizableRootView setFrame:CGRectMake(0, textViewHeight + spacingHeight, self.frame.size.width, _resizableRootView.frame.size.height)];
[_currentSizeTextView setFrame:CGRectMake(0, 0, self.frame.size.width, textViewHeight)];
}
- (NSArray<UIView<RCTComponent> *> *)reactSubviews
{
// this is to avoid unregistering our RCTRootView when the component is removed from RN hierarchy
return @[];
}
#pragma mark - RCTRootViewDelegate
- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView
{
CGRect newFrame = rootView.frame;
newFrame.size = rootView.intrinsicSize;
if (!_sizeUpdated) {
_sizeUpdated = TRUE;
_currentSizeTextView.text = [NSString stringWithFormat:@"RCTRootViewDelegate: content with initially unknown size has appeared, updating root view's size so the content fits."];
} else {
_currentSizeTextView.text = [NSString stringWithFormat:@"RCTRootViewDelegate: content size has been changed to (%ld, %ld), updating root view's size.",
(long)newFrame.size.width,
(long)newFrame.size.height];
}
rootView.frame = newFrame;
}
@end

View File

@@ -88,6 +88,7 @@ RCT_EXPORT_MODULE();
- (NSArray<UIView<RCTComponent> *> *)reactSubviews
{
// this is to avoid unregistering our RCTRootView when the component is removed from RN hierarchy
return @[];
}