2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTWrapperViewController.h"
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
#import <UIKit/UIScrollView.h>
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
|
|
|
#import "RCTNavItem.h"
|
|
|
|
#import "RCTUtils.h"
|
2015-03-06 00:36:41 +00:00
|
|
|
#import "RCTViewControllerProtocol.h"
|
2015-03-26 09:58:06 +00:00
|
|
|
#import "UIView+React.h"
|
2015-09-04 12:50:01 +00:00
|
|
|
#import "RCTAutoInsetsProtocol.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@implementation RCTWrapperViewController
|
|
|
|
{
|
2015-03-06 00:36:41 +00:00
|
|
|
UIView *_wrapperView;
|
2015-02-20 04:10:52 +00:00
|
|
|
UIView *_contentView;
|
2015-09-04 12:50:01 +00:00
|
|
|
RCTEventDispatcher *_eventDispatcher;
|
|
|
|
CGFloat _previousTopLayoutLength;
|
|
|
|
CGFloat _previousBottomLayoutLength;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
@synthesize currentTopLayoutGuide = _currentTopLayoutGuide;
|
|
|
|
@synthesize currentBottomLayoutGuide = _currentBottomLayoutGuide;
|
|
|
|
|
|
|
|
- (instancetype)initWithContentView:(UIView *)contentView
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-06-15 14:53:45 +00:00
|
|
|
RCTAssertParam(contentView);
|
|
|
|
|
|
|
|
if ((self = [super initWithNibName:nil bundle:nil])) {
|
2015-02-20 04:10:52 +00:00
|
|
|
_contentView = contentView;
|
|
|
|
self.automaticallyAdjustsScrollViewInsets = NO;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
- (instancetype)initWithNavItem:(RCTNavItem *)navItem
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
if ((self = [self initWithContentView:navItem])) {
|
2015-02-20 04:10:52 +00:00
|
|
|
_navItem = navItem;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-08-24 10:14:33 +00:00
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithNibName:(NSString *)nn bundle:(NSBundle *)nb)
|
|
|
|
RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
- (void)viewWillLayoutSubviews
|
|
|
|
{
|
|
|
|
[super viewWillLayoutSubviews];
|
|
|
|
|
|
|
|
_currentTopLayoutGuide = self.topLayoutGuide;
|
|
|
|
_currentBottomLayoutGuide = self.bottomLayoutGuide;
|
|
|
|
}
|
|
|
|
|
2015-09-04 12:50:01 +00:00
|
|
|
static BOOL RCTFindScrollViewAndRefreshContentInsetInView(UIView *view)
|
|
|
|
{
|
|
|
|
if ([view conformsToProtocol:@protocol(RCTAutoInsetsProtocol)]) {
|
|
|
|
[(id <RCTAutoInsetsProtocol>) view refreshContentInset];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
for (UIView *subview in view.subviews) {
|
|
|
|
if (RCTFindScrollViewAndRefreshContentInsetInView(subview)) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)viewDidLayoutSubviews
|
|
|
|
{
|
|
|
|
[super viewDidLayoutSubviews];
|
|
|
|
|
|
|
|
if (_previousTopLayoutLength != _currentTopLayoutGuide.length ||
|
|
|
|
_previousBottomLayoutLength != _currentBottomLayoutGuide.length) {
|
|
|
|
RCTFindScrollViewAndRefreshContentInsetInView(_contentView);
|
|
|
|
_previousTopLayoutLength = _currentTopLayoutGuide.length;
|
|
|
|
_previousBottomLayoutLength = _currentBottomLayoutGuide.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-16 11:18:12 +00:00
|
|
|
static UIView *RCTFindNavBarShadowViewInView(UIView *view)
|
|
|
|
{
|
|
|
|
if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height <= 1) {
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
for (UIView *subview in view.subviews) {
|
|
|
|
UIView *shadowView = RCTFindNavBarShadowViewInView(subview);
|
|
|
|
if (shadowView) {
|
|
|
|
return shadowView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)viewWillAppear:(BOOL)animated
|
|
|
|
{
|
|
|
|
[super viewWillAppear:animated];
|
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
// TODO: find a way to make this less-tightly coupled to navigation controller
|
|
|
|
if ([self.parentViewController isKindOfClass:[UINavigationController class]])
|
|
|
|
{
|
2015-04-09 15:52:25 +00:00
|
|
|
[self.navigationController
|
2015-07-16 11:18:12 +00:00
|
|
|
setNavigationBarHidden:_navItem.navigationBarHidden
|
|
|
|
animated:animated];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-06 00:36:41 +00:00
|
|
|
UINavigationBar *bar = self.navigationController.navigationBar;
|
2015-05-07 14:16:59 +00:00
|
|
|
bar.barTintColor = _navItem.barTintColor;
|
|
|
|
bar.tintColor = _navItem.tintColor;
|
2015-06-26 14:06:51 +00:00
|
|
|
bar.translucent = _navItem.translucent;
|
2015-07-16 11:18:12 +00:00
|
|
|
bar.titleTextAttributes = _navItem.titleTextColor ? @{
|
|
|
|
NSForegroundColorAttributeName: _navItem.titleTextColor
|
|
|
|
} : nil;
|
|
|
|
|
|
|
|
RCTFindNavBarShadowViewInView(bar).hidden = _navItem.shadowHidden;
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-05-07 14:16:59 +00:00
|
|
|
UINavigationItem *item = self.navigationItem;
|
|
|
|
item.title = _navItem.title;
|
|
|
|
item.backBarButtonItem = _navItem.backButtonItem;
|
Added mechanism for directly mapping JS event handlers to blocks
Summary:
Currently, the system for mapping JS event handlers to blocks is quite clean on the JS side, but is clunky on the native side. The event property is passed as a boolean, which can then be checked by the native side, and if true, the native side is supposed to send an event via the event dispatcher.
This diff adds the facility to declare the property as a block instead. This means that the event side can simply call the block, and it will automatically send the event. Because the blocks for bubbling and direct events are named differently, we can also use this to generate the event registration data and get rid of the arrays of event names.
The name of the event is inferred from the property name, which means that the property for an event called "load" must be called `onLoad` or the mapping won't work. This can be optionally remapped to a different property name on the view itself if necessary, e.g.
RCT_REMAP_VIEW_PROPERTY(onLoad, loadEventBlock, RCTDirectEventBlock)
If you don't want to use this mechanism then for now it is still possible to declare the property as a BOOL instead and use the old mechanism (this approach is now deprecated however, and may eventually be removed altogether).
2015-09-02 12:58:10 +00:00
|
|
|
item.leftBarButtonItem = _navItem.leftButtonItem;
|
|
|
|
item.rightBarButtonItem = _navItem.rightButtonItem;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)loadView
|
|
|
|
{
|
2015-03-11 02:03:59 +00:00
|
|
|
// Add a wrapper so that the wrapper view managed by the
|
|
|
|
// UINavigationController doesn't end up resetting the frames for
|
2015-03-06 00:36:41 +00:00
|
|
|
//`contentView` which is a react-managed view.
|
|
|
|
_wrapperView = [[UIView alloc] initWithFrame:_contentView.bounds];
|
|
|
|
[_wrapperView addSubview:_contentView];
|
|
|
|
self.view = _wrapperView;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)didMoveToParentViewController:(UIViewController *)parent
|
|
|
|
{
|
2015-03-06 00:36:41 +00:00
|
|
|
// There's no clear setter for navigation controllers, but did move to parent
|
|
|
|
// view controller provides the desired effect. This is called after a pop
|
|
|
|
// finishes, be it a swipe to go back or a standard tap on the back button
|
2015-02-20 04:10:52 +00:00
|
|
|
[super didMoveToParentViewController:parent];
|
|
|
|
if (parent == nil || [parent isKindOfClass:[UINavigationController class]]) {
|
2015-07-16 11:18:12 +00:00
|
|
|
[self.navigationListener wrapperViewController:self
|
|
|
|
didMoveToNavigationController:(UINavigationController *)parent];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|