yogevbd 6 years ago
parent
commit
2c0ec109a8
3 changed files with 29 additions and 76 deletions
  1. 5
    3
      ios/RCCCustomTitleView.h
  2. 22
    68
      ios/RCCCustomTitleView.m
  3. 2
    5
      ios/RCCViewController.m

+ 5
- 3
ios/RCCCustomTitleView.h View File

7
 //
7
 //
8
 
8
 
9
 #import <UIKit/UIKit.h>
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
 @end
18
 @end

+ 22
- 68
ios/RCCCustomTitleView.m View File

9
 #import "RCCCustomTitleView.h"
9
 #import "RCCCustomTitleView.h"
10
 
10
 
11
 @interface RCCCustomTitleView ()
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
 @end
16
 @end
16
 
17
 
17
 @implementation RCCCustomTitleView
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
     if (self) {
24
     if (self) {
25
-        self.backgroundColor = [UIColor clearColor];
26
         self.subView = subView;
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
         [self addSubview:subView];
38
         [self addSubview:subView];
31
     }
39
     }
32
     
40
     
33
     return self;
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
 - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
51
 - (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
92
     // whenever the orientation changes this runs
52
     // whenever the orientation changes this runs
93
     // and sets the nav bar item width to the new size width
53
     // and sets the nav bar item width to the new size width
94
     CGRect newFrame = self.frame;
54
     CGRect newFrame = self.frame;
95
-
55
+    
96
     if (newFrame.size.width < size.width) {
56
     if (newFrame.size.width < size.width) {
97
         newFrame.size.width = size.width;
57
         newFrame.size.width = size.width;
98
         newFrame.origin.x = 0;
58
         newFrame.origin.x = 0;
100
     [super setFrame:newFrame];
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
 @end
63
 @end

+ 2
- 5
ios/RCCViewController.m View File

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