Explorar el Código

Add custom bar button item class (#1641)

This allows for custom React Native views to be rendered as a bar button item in the navigation bar.
Eli Perkins hace 7 años
padre
commit
80ae102456

+ 12
- 0
ios/RCCCustomBarButtonItem.h Ver fichero

@@ -0,0 +1,12 @@
1
+#import <UIKit/UIKit.h>
2
+
3
+@class RCTRootView;
4
+@class RCTBridge;
5
+
6
+@interface RCCCustomBarButtonItem : UIBarButtonItem
7
+
8
+@property (nonnull, nonatomic, strong, readonly) RCTRootView *reactView;
9
+
10
+- (__nonnull instancetype)initWithComponentName:(NSString *__nonnull)component passProps:(NSDictionary *__nullable)passProps bridge:(RCTBridge *__nonnull)bridge;
11
+
12
+@end

+ 30
- 0
ios/RCCCustomBarButtonItem.m Ver fichero

@@ -0,0 +1,30 @@
1
+#import <React/RCTRootView.h>
2
+#import <React/RCTRootViewDelegate.h>
3
+#import "RCCCustomBarButtonItem.h"
4
+
5
+@interface RCCCustomBarButtonItem () <RCTRootViewDelegate>
6
+
7
+@property (nonnull, nonatomic, strong, readwrite) RCTRootView *reactView;
8
+
9
+@end
10
+
11
+@implementation RCCCustomBarButtonItem
12
+
13
+- (instancetype)initWithComponentName:(NSString *__nonnull)component passProps:(NSDictionary *)passProps bridge:(RCTBridge *__nonnull)bridge {
14
+    RCTRootView *reactView = [[RCTRootView alloc] initWithBridge:bridge moduleName:component initialProperties:passProps];
15
+    self = [super initWithCustomView:reactView];
16
+    if (self) {
17
+        reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
18
+        reactView.delegate = self;
19
+        reactView.backgroundColor = [UIColor clearColor];
20
+    }
21
+    return self;
22
+}
23
+
24
+- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
25
+    CGSize size = rootView.intrinsicSize;
26
+    rootView.frame = CGRectMake(0, 0, size.width, size.height);
27
+    self.width = size.width;
28
+}
29
+
30
+@end

+ 8
- 1
ios/RCCNavigationController.m Ver fichero

@@ -3,8 +3,10 @@
3 3
 #import "RCCManager.h"
4 4
 #import <React/RCTEventDispatcher.h>
5 5
 #import <React/RCTConvert.h>
6
+#import <React/RCTRootView.h>
6 7
 #import <objc/runtime.h>
7 8
 #import "RCCTitleViewHelper.h"
9
+#import "RCCCustomBarButtonItem.h"
8 10
 #import "UIViewController+Rotation.h"
9 11
 #import "RCTHelpers.h"
10 12
 
@@ -333,7 +335,8 @@ NSString const *CALLBACK_ASSOCIATED_ID = @"RCCNavigationController.CALLBACK_ASSO
333 335
     UIImage *iconImage = nil;
334 336
     id icon = button[@"icon"];
335 337
     if (icon) iconImage = [RCTConvert UIImage:icon];
336
-    
338
+    NSString *__nullable component = button[@"component"];
339
+
337 340
     UIBarButtonItem *barButtonItem;
338 341
     if (iconImage)
339 342
     {
@@ -348,6 +351,10 @@ NSString const *CALLBACK_ASSOCIATED_ID = @"RCCNavigationController.CALLBACK_ASSO
348 351
         [barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
349 352
       }
350 353
     }
354
+    else if (component) {
355
+      RCTBridge *bridge = [[RCCManager sharedInstance] getBridge];
356
+      barButtonItem = [[RCCCustomBarButtonItem alloc] initWithComponentName:component passProps:button[@"passProps"] bridge:bridge];
357
+    }
351 358
     else continue;
352 359
     objc_setAssociatedObject(barButtonItem, &CALLBACK_ASSOCIATED_KEY, button[@"onPress"], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
353 360
     [barButtonItems addObject:barButtonItem];

+ 6
- 0
ios/ReactNativeNavigation.xcodeproj/project.pbxproj Ver fichero

@@ -11,6 +11,7 @@
11 11
 		261108801E6C495400BF5D98 /* UIViewController+Rotation.m in Sources */ = {isa = PBXBuildFile; fileRef = 2611087F1E6C495400BF5D98 /* UIViewController+Rotation.m */; };
12 12
 		26714EAC1EB0E9D3009F4D52 /* RCCCustomTitleView.m in Sources */ = {isa = PBXBuildFile; fileRef = 26714EAB1EB0E9D3009F4D52 /* RCCCustomTitleView.m */; };
13 13
 		26AFF3F51D7EEE2400CBA211 /* RCCTitleViewHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 26AFF3F41D7EEE2400CBA211 /* RCCTitleViewHelper.m */; };
14
+		2DCD499A1F33AAC30035123A /* RCCCustomBarButtonItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 2DCD49981F33AAC30035123A /* RCCCustomBarButtonItem.m */; };
14 15
 		CC84A19E1C1A0C4E00B3A6A2 /* RCCManager.m in Sources */ = {isa = PBXBuildFile; fileRef = CC84A1941C1A0C4E00B3A6A2 /* RCCManager.m */; };
15 16
 		CC84A19F1C1A0C4E00B3A6A2 /* RCCManagerModule.m in Sources */ = {isa = PBXBuildFile; fileRef = CC84A1961C1A0C4E00B3A6A2 /* RCCManagerModule.m */; };
16 17
 		CC84A1A01C1A0C4E00B3A6A2 /* RCCNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = CC84A1981C1A0C4E00B3A6A2 /* RCCNavigationController.m */; };
@@ -58,6 +59,8 @@
58 59
 		26714EAB1EB0E9D3009F4D52 /* RCCCustomTitleView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCCCustomTitleView.m; sourceTree = "<group>"; };
59 60
 		26AFF3F31D7EEE2400CBA211 /* RCCTitleViewHelper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RCCTitleViewHelper.h; path = Helpers/RCCTitleViewHelper.h; sourceTree = "<group>"; };
60 61
 		26AFF3F41D7EEE2400CBA211 /* RCCTitleViewHelper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RCCTitleViewHelper.m; path = Helpers/RCCTitleViewHelper.m; sourceTree = "<group>"; };
62
+2DCD49981F33AAC30035123A /* RCCCustomBarButtonItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RCCCustomBarButtonItem.m; path = RCCCustomBarButtonItem.m; sourceTree = "<group>"; };
63
+		2DCD49991F33AAC30035123A /* RCCCustomBarButtonItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RCCCustomBarButtonItem.h; path = RCCCustomBarButtonItem.h; sourceTree = "<group>"; };
61 64
 		CC84A1931C1A0C4E00B3A6A2 /* RCCManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCCManager.h; sourceTree = "<group>"; };
62 65
 		CC84A1941C1A0C4E00B3A6A2 /* RCCManager.m */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.c.objc; path = RCCManager.m; sourceTree = "<group>"; tabWidth = 2; };
63 66
 		CC84A1951C1A0C4E00B3A6A2 /* RCCManagerModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCCManagerModule.h; sourceTree = "<group>"; };
@@ -204,6 +207,8 @@
204 207
 				D85082BB1CBCF42400FDB961 /* RCCDrawerController */,
205 208
 				CC84A1931C1A0C4E00B3A6A2 /* RCCManager.h */,
206 209
 				CC84A1941C1A0C4E00B3A6A2 /* RCCManager.m */,
210
+				2DCD49991F33AAC30035123A /* RCCCustomBarButtonItem.h */,
211
+				2DCD49981F33AAC30035123A /* RCCCustomBarButtonItem.m */,
207 212
 				26714EAA1EB0E9D3009F4D52 /* RCCCustomTitleView.h */,
208 213
 				26714EAB1EB0E9D3009F4D52 /* RCCCustomTitleView.m */,
209 214
 				26AFF3F31D7EEE2400CBA211 /* RCCTitleViewHelper.h */,
@@ -303,6 +308,7 @@
303 308
 			files = (
304 309
 				D85082EA1CBCF54200FDB961 /* TheSidebarController.m in Sources */,
305 310
 				CC84A1A01C1A0C4E00B3A6A2 /* RCCNavigationController.m in Sources */,
311
+				2DCD499A1F33AAC30035123A /* RCCCustomBarButtonItem.m in Sources */,
306 312
 				D83514F61D29719A00D53758 /* RCCNotification.m in Sources */,
307 313
 				CC84A19F1C1A0C4E00B3A6A2 /* RCCManagerModule.m in Sources */,
308 314
 				26AFF3F51D7EEE2400CBA211 /* RCCTitleViewHelper.m in Sources */,