mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: This would cause a crash on iOS 9.3 because this method does not exist on UIView on that platform. By wrapping it in an availability check, we provide some safety. Changelog: [iOS] [Fixed] safeAreaInsets call would crash on older versions of iOS Reviewed By: fkgozali Differential Revision: D18118025 fbshipit-source-id: fb7e517b3bcb3e0ba11ae81d8bf8397abc227e04
91 lines
2.3 KiB
Plaintext
91 lines
2.3 KiB
Plaintext
/*
|
|
* 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 "RCTSafeAreaViewComponentView.h"
|
|
|
|
#import <react/components/safeareaview/SafeAreaViewComponentDescriptor.h>
|
|
#import <react/components/safeareaview/SafeAreaViewState.h>
|
|
#import "RCTConversions.h"
|
|
|
|
using namespace facebook::react;
|
|
|
|
static UIScrollView *findScrollView(UIView *view, uint recursionDepth = 0)
|
|
{
|
|
if (recursionDepth >= 3) {
|
|
return NULL;
|
|
}
|
|
if ([view isKindOfClass:[UIScrollView class]]) {
|
|
return (UIScrollView *)view;
|
|
}
|
|
|
|
if (view.subviews.count >= 1) {
|
|
for (UIView *subview in view.subviews) {
|
|
UIScrollView *scrollView = findScrollView(subview.subviews.firstObject, recursionDepth + 1);
|
|
if (scrollView) {
|
|
return scrollView;
|
|
}
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
@implementation RCTSafeAreaViewComponentView {
|
|
SafeAreaViewShadowNode::ConcreteState::Shared _state;
|
|
}
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
if (self = [super initWithFrame:frame]) {
|
|
static const auto defaultProps = std::make_shared<const SafeAreaViewProps>();
|
|
_props = defaultProps;
|
|
self.clipsToBounds = YES;
|
|
}
|
|
|
|
return self;
|
|
}
|
|
|
|
- (UIEdgeInsets)_safeAreaInsets
|
|
{
|
|
if (@available(iOS 11.0, tvOS 11.0, *)) {
|
|
return self.safeAreaInsets;
|
|
}
|
|
|
|
return UIEdgeInsetsZero;
|
|
}
|
|
|
|
- (void)layoutSubviews
|
|
{
|
|
[super layoutSubviews];
|
|
UIScrollView *scrollView = findScrollView(self);
|
|
if (scrollView && CGSizeEqualToSize(scrollView.bounds.size, self.bounds.size)) {
|
|
[scrollView setContentInset:self._safeAreaInsets];
|
|
} else {
|
|
if (_state != nullptr) {
|
|
CGSize size = self.bounds.size;
|
|
size.height -= self._safeAreaInsets.bottom;
|
|
auto newState = SafeAreaViewState{RCTSizeFromCGSize(size)};
|
|
_state->updateState(std::move(newState));
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - RCTComponentViewProtocol
|
|
|
|
- (void)updateState:(facebook::react::State::Shared const &)state
|
|
oldState:(facebook::react::State::Shared const &)oldState
|
|
{
|
|
_state = std::static_pointer_cast<const SafeAreaViewShadowNode::ConcreteState>(state);
|
|
}
|
|
|
|
+ (ComponentDescriptorProvider)componentDescriptorProvider
|
|
{
|
|
return concreteComponentDescriptorProvider<SafeAreaViewComponentDescriptor>();
|
|
}
|
|
|
|
@end
|