mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-06-13 23:09:04 +08:00
Summary: This diff refactors the view update process into two stages: 1. The `reactSubviews` array is set, whose order matches the order of the JS components and shadowView components, as specified by the UIManager. 2. The `didUpdateReactSubviews` method is called, which actually inserts the reactSubviews into the view hierarchy. This simplifies a lot of the hacks we had for special-case treatment of subviews: In many cases we don't want to actually insert `reactSubviews` into the parentView, and we had a bunch of component-specific solutions for that (typically overriding all of the reactSubviews methods to store views in an array). Now, we can simply override the `didUpdateReactSubviews` method for those views to do nothing, or do something different. Reviewed By: wwjholmes Differential Revision: D3396594 fbshipit-source-id: 92fc56fd31db0cfc66aac3d1634a4d4ae3903085
97 lines
2.7 KiB
Objective-C
97 lines
2.7 KiB
Objective-C
/**
|
|
* 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 "UpdatePropertiesExampleView.h"
|
|
#import "RCTJavaScriptLoader.h"
|
|
#import "RCTBridge.h"
|
|
#import "RCTRootView.h"
|
|
#import "RCTViewManager.h"
|
|
|
|
@interface UpdatePropertiesExampleViewManager : RCTViewManager
|
|
|
|
@end
|
|
|
|
@implementation UpdatePropertiesExampleViewManager
|
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
- (UIView *)view
|
|
{
|
|
return [UpdatePropertiesExampleView new];
|
|
}
|
|
|
|
@end
|
|
|
|
@implementation UpdatePropertiesExampleView
|
|
{
|
|
RCTRootView *_rootView;
|
|
UIButton *_button;
|
|
BOOL _beige;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if (self) {
|
|
_beige = YES;
|
|
|
|
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
|
|
|
|
_rootView = [[RCTRootView alloc] initWithBridge:appDelegate.bridge
|
|
moduleName:@"SetPropertiesExampleApp"
|
|
initialProperties:@{@"color":@"beige"}];
|
|
|
|
_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
|
|
[_button setTitle:@"Native Button" forState:UIControlStateNormal];
|
|
[_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
|
|
[_button setBackgroundColor:[UIColor grayColor]];
|
|
|
|
[_button addTarget:self
|
|
action:@selector(changeColor)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
|
|
[self addSubview:_button];
|
|
[self addSubview:_rootView];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)layoutSubviews
|
|
{
|
|
float spaceHeight = 20;
|
|
float buttonHeight = 40;
|
|
float rootViewWidth = self.bounds.size.width;
|
|
float rootViewHeight = self.bounds.size.height - spaceHeight - buttonHeight;
|
|
|
|
[_rootView setFrame:CGRectMake(0, 0, rootViewWidth, rootViewHeight)];
|
|
[_button setFrame:CGRectMake(0, rootViewHeight + spaceHeight, rootViewWidth, buttonHeight)];
|
|
}
|
|
|
|
- (void)changeColor
|
|
{
|
|
_beige = !_beige;
|
|
[_rootView setAppProperties:@{@"color":_beige ? @"beige" : @"purple"}];
|
|
}
|
|
|
|
- (NSArray<UIView<RCTComponent> *> *)reactSubviews
|
|
{
|
|
// this is to avoid unregistering our RCTRootView when the component is removed from RN hierarchy
|
|
(void)[super reactSubviews];
|
|
return @[];
|
|
}
|
|
|
|
@end
|