yogevbd 6 年前
父节点
当前提交
2c0ec109a8
共有 3 个文件被更改,包括 29 次插入76 次删除
  1. 5
    3
      ios/RCCCustomTitleView.h
  2. 22
    68
      ios/RCCCustomTitleView.m
  3. 2
    5
      ios/RCCViewController.m

+ 5
- 3
ios/RCCCustomTitleView.h 查看文件

@@ -7,10 +7,12 @@
7 7
 //
8 8
 
9 9
 #import <UIKit/UIKit.h>
10
+#import <React/RCTRootView.h>
11
+#import <React/RCTRootViewDelegate.h>
10 12
 
11
-@interface RCCCustomTitleView : UIView
13
+@interface RCCCustomTitleView : UIView <RCTRootViewDelegate>
12 14
 
13
--(instancetype)initWithFrame:(CGRect)frame subView:(UIView*)subView alignment:(NSString*)alignment;
14
-- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator;
15
+- (instancetype)initWithFrame:(CGRect)frame subView:(RCTRootView*)subView alignment:(NSString*)alignment;
16
+- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator;
15 17
 
16 18
 @end

+ 22
- 68
ios/RCCCustomTitleView.m 查看文件

@@ -9,90 +9,50 @@
9 9
 #import "RCCCustomTitleView.h"
10 10
 
11 11
 @interface RCCCustomTitleView ()
12
-@property (nonatomic, strong) UIView *subView;
13
-@property (nonatomic, strong) NSString *subViewAlign;
14
-@property float initialWidth;
12
+
13
+@property (nonatomic, strong) RCTRootView *subView;
14
+@property (nonatomic, strong) NSString *alignment;
15
+
15 16
 @end
16 17
 
17 18
 @implementation RCCCustomTitleView
18 19
 
19 20
 
20
--(instancetype)initWithFrame:(CGRect)frame subView:(UIView*)subView alignment:(NSString*)alignment {
21
-    _initialWidth = frame.size.width;
22
-    self = [super initWithFrame:frame];
21
+- (instancetype)initWithFrame:(CGRect)frame subView:(RCTRootView*)subView alignment:(NSString*)alignment {
22
+    self = [super init];
23 23
     
24 24
     if (self) {
25
-        self.backgroundColor = [UIColor clearColor];
26 25
         self.subView = subView;
27
-        self.subViewAlign = alignment;
26
+        self.alignment = alignment;
27
+        
28
+        self.backgroundColor = [UIColor clearColor];
29
+        self.subView.backgroundColor = [UIColor clearColor];
30
+        
31
+        if ([alignment isEqualToString:@"fill"]) {
32
+            self.frame = frame;
33
+            self.subView.frame = self.frame;
34
+        } else {
35
+            self.subView.delegate = self;
36
+        }
28 37
         
29
-        subView.frame = self.bounds;
30 38
         [self addSubview:subView];
31 39
     }
32 40
     
33 41
     return self;
34 42
 }
35 43
 
36
-
37
--(void)layoutSubviews {
38
-    [super layoutSubviews];
39
-    
40
-    if ([self.subViewAlign isEqualToString:@"fill"]) {
41
-        self.subView.frame = self.bounds;
42
-    }
43
-    else {
44
-        
45
-        CGFloat superViewWidth = self.superview.frame.size.width;
46
-        CGFloat paddingLeftFromCenter = (superViewWidth/2) - self.frame.origin.x;
47
-        CGFloat paddingRightFromCenter = self.frame.size.width - paddingLeftFromCenter;;
48
-        CGRect reactViewFrame = self.subView.bounds;
49
-        CGFloat minPadding = MIN(paddingLeftFromCenter, paddingRightFromCenter);
50
-        
51
-        reactViewFrame.size.width = minPadding*2;
52
-        reactViewFrame.origin.x = paddingLeftFromCenter - minPadding;
53
-        self.subView.frame = reactViewFrame;
44
+- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
45
+    if ([self.alignment isEqualToString:@"center"]) {
46
+        [self setFrame:CGRectMake(0, 0, rootView.intrinsicContentSize.width, rootView.intrinsicContentSize.height)];
47
+        [self.subView setFrame:CGRectMake(0, 0, rootView.intrinsicContentSize.width, rootView.intrinsicContentSize.height)];
54 48
     }
55 49
 }
56 50
 
57
-- (void)setFrame:(CGRect) frame {
58
-    float referenceWidth = [self statusBarWidth];
59
-    if (referenceWidth == 0) {
60
-        referenceWidth = _initialWidth;
61
-    }
62
-    float newNavBarWidth = frame.size.width;
63
-    BOOL frameNeedsToBeCorrected = newNavBarWidth < referenceWidth || CGRectEqualToRect(self.frame, CGRectZero);
64
-
65
-    if (frameNeedsToBeCorrected) {
66
-        // first we need to find out the total point diff of the status bar and the nav bar
67
-        float navBarHorizontalMargin = referenceWidth - newNavBarWidth;
68
-        
69
-        CGRect correctedFrame = frame;
70
-
71
-        // then we need to place the nav bar half times the horizontal margin to the left
72
-        correctedFrame.origin.x = -(navBarHorizontalMargin / 2);
73
-        
74
-        // and finally set the width so that it's equal to the status bar width
75
-        correctedFrame.size.width = referenceWidth;
76
-        
77
-        [super setFrame:correctedFrame];
78
-    } else if (frame.size.height != self.frame.size.height) { // otherwise
79
-        // if only the height has changed
80
-        CGRect newHeightFrame = self.frame;
81
-        // make sure we update just the height
82
-        newHeightFrame.size.height = frame.size.height;
83
-        [super setFrame:newHeightFrame];
84
-    }
85
-    
86
-    // keep a ref to the last frame, so that we avoid setting the frame twice for no reason
87
-//    _lastFrame = frame;
88
-}
89
-
90
-
91 51
 - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
92 52
     // whenever the orientation changes this runs
93 53
     // and sets the nav bar item width to the new size width
94 54
     CGRect newFrame = self.frame;
95
-
55
+    
96 56
     if (newFrame.size.width < size.width) {
97 57
         newFrame.size.width = size.width;
98 58
         newFrame.origin.x = 0;
@@ -100,10 +60,4 @@
100 60
     [super setFrame:newFrame];
101 61
 }
102 62
 
103
--(float) statusBarWidth {
104
-    CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
105
-    return MAX(statusBarSize.width, statusBarSize.height);
106
-}
107
-
108
-
109 63
 @end

+ 2
- 5
ios/RCCViewController.m 查看文件

@@ -642,17 +642,14 @@ const NSInteger TRANSPARENT_NAVBAR_TAG = 78264803;
642 642
       
643 643
       NSDictionary *initialProps = self.navigatorStyle[@"navBarCustomViewInitialProps"];
644 644
       RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:navBarCustomView initialProperties:initialProps];
645
-      
645
+      reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
646
+        
646 647
       RCCCustomTitleView *titleView = [[RCCCustomTitleView alloc] initWithFrame:self.navigationController.navigationBar.bounds
647 648
                                                                         subView:reactView
648 649
                                                                       alignment:self.navigatorStyle[@"navBarComponentAlignment"]];
649
-      titleView.backgroundColor = [UIColor clearColor];
650
-      reactView.backgroundColor = [UIColor clearColor];
651 650
       
652 651
       self.navigationItem.titleView = titleView;
653
-      
654 652
       self.navigationItem.titleView.backgroundColor = [UIColor clearColor];
655
-      self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
656 653
       self.navigationItem.titleView.clipsToBounds = YES;
657 654
     }
658 655
   }