Skip to content

Commit 7a26ea9

Browse files
BerndSchrootenguyca
authored andcommitted
Support iOS system item icons for top bar (#4547)
1 parent 54569ee commit 7a26ea9

8 files changed

+95
-0
lines changed

docs/docs/topBar-buttons.md

+30
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The following options can be used to customise buttons.
1010
name: 'example.CustomButtonComponent'
1111
},
1212
text: 'Button one',
13+
systemItem: 'done', // iOS only. Sets a system bar button item as the icon. Matches UIBarButtonSystemItem naming. See below for details.
1314
enabled: true,
1415
disableIconTint: false,
1516
color: 'red',
@@ -18,6 +19,35 @@ The following options can be used to customise buttons.
1819
}
1920
```
2021

22+
## iOS System Items
23+
On iOS, UIKit supplies some common bar button glyphs for developers to use. The following values can be supplied as values to to `systemItem` to use them as an icon for your button.
24+
25+
* `done`
26+
* `cancel`
27+
* `edit`
28+
* `save`
29+
* `add`
30+
* `flexibleSpace`
31+
* `fixedSpace`
32+
* `compose`
33+
* `reply`
34+
* `action`
35+
* `organize`
36+
* `bookmarks`
37+
* `search`
38+
* `refresh`
39+
* `stop`
40+
* `camera`
41+
* `trash`
42+
* `play`
43+
* `pause`
44+
* `rewind`
45+
* `fastForward`
46+
* `undo`
47+
* `redo`
48+
49+
More information about these glyphs can be found in [Apple's Human Interface Guidelines](https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/system-icons/).
50+
2151
# Declaring Buttons statically
2252

2353
Buttons can be defined in a screen's options:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import <UIKit/UIKit.h>
2+
#import <React/RCTConvert.h>
3+
4+
@interface RCTConvert (UIBarButtonSystemItem)
5+
6+
+ (UIBarButtonSystemItem)UIBarButtonSystemItem:(id)json;
7+
8+
@end
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#import <UIKit/UIKit.h>
2+
#import "RCTConvert+UIBarButtonSystemItem.h"
3+
4+
@implementation RCTConvert (UIBarButtonSystemItem)
5+
6+
RCT_ENUM_CONVERTER(UIBarButtonSystemItem, (@{
7+
@"done" : @(UIBarButtonSystemItemDone),
8+
@"cancel" : @(UIBarButtonSystemItemCancel),
9+
@"edit" : @(UIBarButtonSystemItemEdit),
10+
@"save" : @(UIBarButtonSystemItemSave),
11+
@"add" : @(UIBarButtonSystemItemAdd),
12+
@"flexibleSpace" : @(UIBarButtonSystemItemFlexibleSpace),
13+
@"fixedSpace" : @(UIBarButtonSystemItemFixedSpace),
14+
@"compose" : @(UIBarButtonSystemItemCompose),
15+
@"reply" : @(UIBarButtonSystemItemReply),
16+
@"action" : @(UIBarButtonSystemItemAction),
17+
@"organize" : @(UIBarButtonSystemItemOrganize),
18+
@"bookmarks" : @(UIBarButtonSystemItemBookmarks),
19+
@"search" : @(UIBarButtonSystemItemSearch),
20+
@"refresh" : @(UIBarButtonSystemItemRefresh),
21+
@"stop" : @(UIBarButtonSystemItemStop),
22+
@"camera" : @(UIBarButtonSystemItemCamera),
23+
@"trash" : @(UIBarButtonSystemItemTrash),
24+
@"play" : @(UIBarButtonSystemItemPlay),
25+
@"pause" : @(UIBarButtonSystemItemPause),
26+
@"rewind" : @(UIBarButtonSystemItemRewind),
27+
@"fastForward" : @(UIBarButtonSystemItemFastForward),
28+
@"undo" : @(UIBarButtonSystemItemUndo),
29+
@"redo" : @(UIBarButtonSystemItemRedo),
30+
}), UIBarButtonSystemItemDone, integerValue)
31+
32+
@end

lib/ios/RNNNavigationButtons.m

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNBu
6767
NSString* buttonId = dictionary[@"id"];
6868
NSString* title = [self getValue:dictionary[@"text"] withDefault:[defaultStyle.text getWithDefaultValue:nil]];
6969
NSDictionary* component = dictionary[@"component"];
70+
NSString* systemItemName = dictionary[@"systemItem"];
7071

7172
if (!buttonId) {
7273
@throw [NSException exceptionWithName:@"NSInvalidArgumentException" reason:[@"button id is not specified " stringByAppendingString:title] userInfo:nil];
@@ -91,6 +92,8 @@ -(RNNUIBarButtonItem*)buildButton: (NSDictionary*)dictionary defaultStyle:(RNNBu
9192
if (buttonTextAttributes.allKeys.count > 0) {
9293
[barButtonItem setTitleTextAttributes:buttonTextAttributes forState:UIControlStateNormal];
9394
}
95+
} else if (systemItemName) {
96+
barButtonItem = [[RNNUIBarButtonItem alloc] init:buttonId withSystemItem:systemItemName];
9497
} else {
9598
return nil;
9699
}

lib/ios/RNNUIBarButtonItem.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
-(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage;
1010
-(instancetype)init:(NSString*)buttonId withTitle:(NSString*)title;
1111
-(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView*)reactView;
12+
-(instancetype)init:(NSString*)buttonId withSystemItem:(NSString*)systemItemName;
1213

1314
@end
1415

lib/ios/RNNUIBarButtonItem.m

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#import <Foundation/Foundation.h>
22
#import <UIKit/UIKit.h>
33
#import "RNNUIBarButtonItem.h"
4+
#import "RCTConvert+UIBarButtonSystemItem.h"
45

56
@implementation RNNUIBarButtonItem
67

@@ -25,6 +26,13 @@ -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView
2526
self.buttonId = buttonId;
2627
return self;
2728
}
29+
30+
-(instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
31+
UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
32+
self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
33+
self.buttonId = buttonId;
34+
return self;
35+
}
2836

2937
- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
3038
CGSize size = rootView.intrinsicContentSize;

lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@
235235
50F5DFC21F407A8C001A00BC /* RNNTabBarController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50F5DFC01F407A8C001A00BC /* RNNTabBarController.m */; };
236236
50F5DFC51F407AA0001A00BC /* RNNNavigationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 50F5DFC31F407AA0001A00BC /* RNNNavigationController.h */; };
237237
50F5DFC61F407AA0001A00BC /* RNNNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50F5DFC41F407AA0001A00BC /* RNNNavigationController.m */; };
238+
7365071121E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 7365070F21E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h */; };
239+
7365071221E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 7365071021E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m */; };
238240
7B1126A01E2D263F00F9B03B /* RNNEventEmitter.m in Sources */ = {isa = PBXBuildFile; fileRef = 7B11269F1E2D263F00F9B03B /* RNNEventEmitter.m */; };
239241
7B1126A31E2D2B6C00F9B03B /* RNNSplashScreen.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BA500761E254908001B9E1B /* RNNSplashScreen.h */; };
240242
7B1126A61E2D2B6C00F9B03B /* RNNBridgeModule.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BBFE5421E25330E002A6182 /* RNNBridgeModule.h */; };
@@ -558,6 +560,8 @@
558560
50F5DFC01F407A8C001A00BC /* RNNTabBarController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNTabBarController.m; sourceTree = "<group>"; };
559561
50F5DFC31F407AA0001A00BC /* RNNNavigationController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNNavigationController.h; sourceTree = "<group>"; };
560562
50F5DFC41F407AA0001A00BC /* RNNNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNNavigationController.m; sourceTree = "<group>"; };
563+
7365070F21E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+UIBarButtonSystemItem.h"; sourceTree = "<group>"; };
564+
7365071021E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+UIBarButtonSystemItem.m"; sourceTree = "<group>"; };
561565
7B11269E1E2D263F00F9B03B /* RNNEventEmitter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNEventEmitter.h; sourceTree = "<group>"; };
562566
7B11269F1E2D263F00F9B03B /* RNNEventEmitter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNNEventEmitter.m; sourceTree = "<group>"; };
563567
7B4928061E70415400555040 /* RNNCommandsHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNNCommandsHandler.h; sourceTree = "<group>"; };
@@ -693,6 +697,8 @@
693697
5038A3C0216E1E66009280BC /* RNNFontAttributesCreator.m */,
694698
505EDD48214FDA800071C7DE /* RCTConvert+Modal.h */,
695699
50E02BD521A6E54B00A43942 /* RCTConvert+SideMenuOpenGestureMode.h */,
700+
7365070F21E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h */,
701+
7365071021E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m */,
696702
5038A3BB216E1490009280BC /* RNNTabBarItemCreator.h */,
697703
5038A3BC216E1490009280BC /* RNNTabBarItemCreator.m */,
698704
5053CE7D2175FB1900D0386B /* RNNDefaultOptionsHelper.h */,
@@ -1186,6 +1192,7 @@
11861192
50495942216F5E5D006D2B81 /* NullBool.h in Headers */,
11871193
263905AE1E4C6F440023D7D3 /* MMDrawerBarButtonItem.h in Headers */,
11881194
5012241621736667000F5F98 /* Color.h in Headers */,
1195+
7365071121E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.h in Headers */,
11891196
5064495D20DC62B90026709C /* RNNSideMenuSideOptions.h in Headers */,
11901197
50F5DFC11F407A8C001A00BC /* RNNTabBarController.h in Headers */,
11911198
50395587217480C900B0A663 /* IntNumber.h in Headers */,
@@ -1418,6 +1425,7 @@
14181425
263905B41E4C6F440023D7D3 /* MMDrawerVisualState.m in Sources */,
14191426
5012240B21735959000F5F98 /* RNNSideMenuPresenter.m in Sources */,
14201427
502CB46F20CD1DDA0019B2FE /* RNNBackButtonOptions.m in Sources */,
1428+
7365071221E4B16F004E020F /* RCTConvert+UIBarButtonSystemItem.m in Sources */,
14211429
5012241B21736678000F5F98 /* Image.m in Sources */,
14221430
50495943216F5E5D006D2B81 /* NullBool.m in Sources */,
14231431
5038A3C7216E2D93009280BC /* Number.m in Sources */,

lib/src/interfaces/Options.ts

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type Color = string;
55
type FontFamily = string;
66
type LayoutOrientation = 'portrait' | 'landscape';
77
type AndroidDensityNumber = number;
8+
type SystemItemIcon = 'done' | 'cancel' | 'edit' | 'save' | 'add' | 'flexibleSpace' | 'fixedSpace' | 'compose' | 'reply' | 'action' | 'organize' | 'bookmarks' | 'search' | 'refresh' | 'stop' | 'camera' | 'trash' | 'play' | 'pause' | 'rewind' | 'fastForward' | 'undo' | 'redo';
89

910
export interface OptionsSplitView {
1011
/**
@@ -256,6 +257,10 @@ export interface OptionsTopBarButton {
256257
*/
257258
passProps?: object;
258259
};
260+
/**
261+
* (iOS only) Set the button as an iOS system icon
262+
*/
263+
systemItem?: SystemItemIcon;
259264
/**
260265
* Set the button text
261266
*/

0 commit comments

Comments
 (0)