Skip to content

Commit

Permalink
[Nav] Add support for bar button icons and left buttons
Browse files Browse the repository at this point in the history
NavigatorIOS supports four new properties:

  - **rightButtonImageSource:** The source of an image to display in the top right. This must be a static image since UINavigationController only supports UIImages. Adding support for UIImageViews (or arbitrary views) is more complicated because custom views do not fade on touch and do not have hit slop the same way that UIImage buttons do. Usage: `rightButtonImageSource: ix('ImageName')`
  - **backButtonImageSource:** Use a custom image for the back button. This does not replace the back caret (`<`) but instead replaces the text next to it.
  - **leftButtonTitle**: Text for the left nav button, which supersedes the previous nav item's back button when specified. The main use case for this is your initial screen/UIVC which has nothing to go back to (since it is the first VC on the stack) but need to display a left button. This does hide the back button if there would have been one otherwise.
  - **leftButtonImageSource:** Image source for the left button, supersedes the left button title.

Added UIExplorer example to demonstrate.
  • Loading branch information
ide committed Mar 31, 2015
1 parent c8c4b41 commit 63dbe44
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Examples/UIExplorer/ImageMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ declare module 'image!uie_thumb_selected' {
declare var uri: string;
declare var isStatic: boolean;
}

declare module 'image!NavBarButtonPlus' {
declare var uri: string;
declare var isStatic: boolean;
}
25 changes: 25 additions & 0 deletions Examples/UIExplorer/NavigatorIOSExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
var React = require('react-native');
var ViewExample = require('./ViewExample');
var {
AlertIOS,
PixelRatio,
ScrollView,
StyleSheet,
Expand Down Expand Up @@ -91,6 +92,30 @@ var NavigatorIOSExample = React.createClass({
}
});
})}
{this._renderRow('Custom Left & Right Icons', () => {
this.props.navigator.push({
title: NavigatorIOSExample.title,
component: EmptyPage,
leftButtonTitle: 'Custom Left',
onLeftButtonPress: () => this.props.navigator.pop(),
rightButtonImageSource: require('image!NavBarButtonPlus'),
onRightButtonPress: () => {
AlertIOS.alert(
'Bar Button Action',
'Recognized a tap on the bar button icon',
[
{
text: 'OK',
onPress: () => console.log('Tapped OK'),
},
]
);
},
passProps: {
text: 'This page has an icon for the right button in the nav bar',
}
});
})}
{this._renderRow('Pop', () => {
this.props.navigator.pop();
})}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x",
"filename" : "NavBarButtonPlus@3x.png"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions Libraries/Components/Navigation/NavigatorIOS.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

var EventEmitter = require('EventEmitter');
var Image = require('Image');
var React = require('React');
var ReactIOSViewAttributes = require('ReactIOSViewAttributes');
var RCTNavigatorManager = require('NativeModules').NavigatorManager;
Expand Down Expand Up @@ -47,9 +48,14 @@ var RCTNavigatorItem = createReactIOSNativeComponentClass({
// NavigatorIOS does not use them all, because some are problematic
title: true,
barTintColor: true,
leftButtonImageName: true,
leftButtonTitle: true,
onNavLeftButtonTap: true,
rightButtonImageName: true,
rightButtonTitle: true,
onNavRightButtonTap: true,
tintColor: true,
backButtonImageName: true,
backButtonTitle: true,
titleTextColor: true,
style: true,
Expand Down Expand Up @@ -78,7 +84,12 @@ type Route = {
title: string;
passProps: Object;
backButtonTitle: string;
backButtonImageSource: Object;
leftButtonTitle: string;
leftButtonImageSource: Object;
onLeftButtonPress: Function;
rightButtonTitle: string;
rightButtonImageSource: Object;
onRightButtonPress: Function;
wrapperStyle: any;
};
Expand Down Expand Up @@ -211,13 +222,40 @@ var NavigatorIOS = React.createClass({
*/
passProps: PropTypes.object,

/**
* If set, the left header button image will appear with this source. Note
* that this doesn't apply for the header of the current view, but the
* ones of the views that are pushed afterward.
*/
backButtonImageSource: Image.propTypes.source,

/**
* If set, the left header button will appear with this name. Note that
* this doesn't apply for the header of the current view, but the ones
* of the views that are pushed afterward.
*/
backButtonTitle: PropTypes.string,

/**
* If set, the left header button image will appear with this source
*/
leftButtonImageSource: Image.propTypes.source,

/**
* If set, the left header button will appear with this name
*/
leftButtonTitle: PropTypes.string,

/**
* Called when the left header button is pressed
*/
onLeftButtonPress: PropTypes.func,

/**
* If set, the right header button image will appear with this source
*/
rightButtonImageSource: Image.propTypes.source,

/**
* If set, the right header button will appear with this name
*/
Expand Down Expand Up @@ -544,7 +582,12 @@ var NavigatorIOS = React.createClass({
this.props.itemWrapperStyle,
route.wrapperStyle
]}
backButtonImageName={this._imageNameFromSource(route.backButtonImageSource)}
backButtonTitle={route.backButtonTitle}
leftButtonImageName={this._imageNameFromSource(route.leftButtonImageSource)}
leftButtonTitle={route.leftButtonTitle}
onNavLeftButtonTap={route.onLeftButtonPress}
rightButtonImageName={this._imageNameFromSource(route.rightButtonImageSource)}
rightButtonTitle={route.rightButtonTitle}
onNavRightButtonTap={route.onRightButtonPress}
tintColor={this.props.tintColor}>
Expand All @@ -558,6 +601,10 @@ var NavigatorIOS = React.createClass({
);
},

_imageNameFromSource: function(source: ?Object) {
return source ? source.uri : undefined;
},

renderNavigationStackItems: function() {
var shouldRecurseToNavigator =
this.state.makingNavigatorRequest ||
Expand Down
6 changes: 6 additions & 0 deletions React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,12 @@ - (NSDictionary *)customBubblingEventTypes
@"captured": @"onNavigationCompleteCapture"
}
},
@"topNavLeftButtonTap": @{
@"phasedRegistrationNames": @{
@"bubbled": @"onNavLeftButtonTap",
@"captured": @"onNavLefttButtonTapCapture"
}
},
@"topNavRightButtonTap": @{
@"phasedRegistrationNames": @{
@"bubbled": @"onNavRightButtonTap",
Expand Down
4 changes: 4 additions & 0 deletions React/Views/RCTNavItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
@interface RCTNavItem : UIView

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) UIImage *leftButtonImage;
@property (nonatomic, copy) NSString *leftButtonTitle;
@property (nonatomic, copy) UIImage *rightButtonImage;
@property (nonatomic, copy) NSString *rightButtonTitle;
@property (nonatomic, copy) UIImage *backButtonImage;
@property (nonatomic, copy) NSString *backButtonTitle;
@property (nonatomic, copy) UIColor *tintColor;
@property (nonatomic, copy) UIColor *barTintColor;
Expand Down
16 changes: 16 additions & 0 deletions React/Views/RCTNavItemManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,26 @@ - (UIView *)view
}

RCT_EXPORT_VIEW_PROPERTY(title, NSString)
RCT_EXPORT_VIEW_PROPERTY(leftButtonTitle, NSString);
RCT_EXPORT_VIEW_PROPERTY(rightButtonTitle, NSString);
RCT_EXPORT_VIEW_PROPERTY(backButtonTitle, NSString);
RCT_EXPORT_VIEW_PROPERTY(tintColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(barTintColor, UIColor);
RCT_EXPORT_VIEW_PROPERTY(titleTextColor, UIColor);

- (void)set_leftButtonImageName:(id)json forView:(RCTNavItem *)view withDefaultView:(RCTNavItem *)defaultView
{
view.leftButtonImage = json ? [RCTConvert UIImage:json] : defaultView.leftButtonImage;
}

- (void)set_rightButtonImageName:(id)json forView:(RCTNavItem *)view withDefaultView:(RCTNavItem *)defaultView
{
view.rightButtonImage = json ? [RCTConvert UIImage:json] : defaultView.rightButtonImage;
}

- (void)set_backButtonImageName:(id)json forView:(RCTNavItem *)view withDefaultView:(RCTNavItem *)defaultView
{
view.backButtonImage = json ? [RCTConvert UIImage:json] : defaultView.backButtonImage;
}

@end
46 changes: 39 additions & 7 deletions React/Views/RCTWrapperViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,43 @@ - (void)viewWillAppear:(BOOL)animated
[bar setTitleTextAttributes:@{NSForegroundColorAttributeName : _navItem.titleTextColor}];
}

if (_navItem.rightButtonTitle.length > 0) {
if (_navItem.leftButtonImage) {
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:_navItem.leftButtonImage
style:UIBarButtonItemStylePlain
target:self
action:@selector(handleNavLeftButtonTapped)];
} else if (_navItem.leftButtonTitle.length > 0) {
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:_navItem.leftButtonTitle
style:UIBarButtonItemStylePlain
target:self
action:@selector(handleNavLeftButtonTapped)];
}

if (_navItem.rightButtonImage) {
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:_navItem.rightButtonTitle
style:UIBarButtonItemStyleDone
target:self
action:@selector(handleNavRightButtonTapped)];
[[UIBarButtonItem alloc] initWithImage:_navItem.rightButtonImage
style:UIBarButtonItemStylePlain
target:self
action:@selector(handleNavRightButtonTapped)];
} else if (_navItem.rightButtonTitle.length > 0) {
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:_navItem.rightButtonTitle
style:UIBarButtonItemStyleDone
target:self
action:@selector(handleNavRightButtonTapped)];
}

if (_navItem.backButtonTitle.length > 0) {
if (_navItem.backButtonImage) {
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithImage:_navItem.backButtonImage
style:UIBarButtonItemStylePlain
target:nil
action:nil];
} else if (_navItem.backButtonTitle.length > 0) {
self.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:_navItem.backButtonTitle
[[UIBarButtonItem alloc] initWithTitle:_navItem.backButtonTitle
style:UIBarButtonItemStylePlain
target:nil
action:nil];
Expand All @@ -114,6 +140,12 @@ - (void)loadView
self.view = _wrapperView;
}

- (void)handleNavLeftButtonTapped
{
[_eventDispatcher sendInputEventWithName:@"topNavLeftButtonTap"
body:@{@"target":_navItem.reactTag}];
}

- (void)handleNavRightButtonTapped
{
[_eventDispatcher sendInputEventWithName:@"topNavRightButtonTap"
Expand Down

0 comments on commit 63dbe44

Please sign in to comment.