Skip to content

Commit a8c2829

Browse files
committed
bug修复
我发现我不能把它和tabbar完美结合,所以我只能另寻其他办法
1 parent 920605f commit a8c2829

File tree

12 files changed

+393
-35
lines changed

12 files changed

+393
-35
lines changed

index.js

Lines changed: 113 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import React, {
1010
View,
1111
Component,
1212
Platform,
13+
Text,
1314
} from 'react-native';
1415

1516
import NavBarContainer from './src/components/NavBarContainer';
17+
import Layout from './src/components/Layout';
18+
import Tabitem from './src/components/Tabitem';
19+
import Tab from './src/components/Tab';
20+
import TabBar from './src/components/TabBar';
21+
import Badge from './src/components/Badge';
1622

17-
export default class Tabs extends Component {
23+
export default class Nav extends Component {
1824

1925

2026
constructor (props) {
@@ -27,22 +33,45 @@ export default class Tabs extends Component {
2733
dragStartX: null,
2834
didSwitchView: null,
2935
}
30-
this.onDidFocus = this.onDidFocus.bind(this);
3136
this.onBack = this.onBack.bind(this);
3237
this.onForward = this.onForward.bind(this);
3338
this.customAction = this.customAction.bind(this);
3439
this.renderScene = this.renderScene.bind(this);
40+
this.onDidFocus = this.onDidFocus.bind(this);
3541
}
3642

3743
/*
3844
* This changes the title in the navigation bar
3945
* It should preferrably be called for "onWillFocus" instad >
4046
* > but a recent update to React Native seems to break the animation
47+
* onDidFocus 已废弃
4148
*/
4249
onDidFocus (route) {
4350
this.setState({ route: route });
4451
}
4552

53+
54+
// _setNavigatorRef (navigator) {
55+
// if (navigator !== this._navigator) {
56+
// this._navigator = navigator;
57+
//
58+
// if (navigator) {
59+
// var callback = (event) => {
60+
// this.setState({ route: route });
61+
// };
62+
// // Observe focus change events from the owner.
63+
// this._listeners = [
64+
// navigator.navigationContext.addListener('willfocus', callback),
65+
// navigator.navigationContext.addListener('didfocus', callback),
66+
// ];
67+
// }
68+
// }
69+
// }
70+
//
71+
// componentWillUnmount () {
72+
// this._listeners && this._listeners.forEach(listener => listener.remove());
73+
// }
74+
4675
onBack (navigator) {
4776
if (this.state.route.index > 0) {
4877
navigator.pop();
@@ -58,6 +87,8 @@ export default class Tabs extends Component {
5887
this.props.customAction(opts);
5988
}
6089

90+
91+
6192
renderScene (route, navigator) {
6293

6394
var goForward = function(route) {
@@ -102,6 +133,9 @@ export default class Tabs extends Component {
102133
return true;
103134
};
104135

136+
137+
138+
105139
var Content = route.component;
106140

107141
// Remove the margin of the navigation bar if not using navigation bar
@@ -125,6 +159,7 @@ export default class Tabs extends Component {
125159
reset={goToFirstRoute}
126160
customAction={customAction}
127161
/>
162+
128163
</View>
129164
)
130165

@@ -159,21 +194,89 @@ export default class Tabs extends Component {
159194
/>
160195
}
161196

197+
198+
//toRoute in Tab
199+
let _renderTab = function (item) {
200+
let icon;
201+
if (item.props.selected) {
202+
if (item.props.renderSelectedIcon) {
203+
icon = item.props.renderSelectedIcon();
204+
} else if (item.props.renderIcon) {
205+
let defaultIcon = item.props.renderIcon();
206+
icon = React.cloneElement(defaultIcon, {
207+
style: [defaultIcon.props.style, styles.defaultSelectedIcon],
208+
});
209+
}
210+
} else if (item.props.renderIcon) {
211+
icon = item.props.renderIcon();
212+
}
213+
214+
let badge;
215+
if (item.props.renderBadge) {
216+
badge = item.props.renderBadge();
217+
} else if (item.props.badgeText) {
218+
badge = <Badge>{item.props.badgeText}</Badge>;
219+
}
220+
return (
221+
<Tab
222+
changeRoute={item.props.changeRoute}
223+
title={item.props.title}
224+
titleStyle={[
225+
item.props.titleStyle,
226+
item.props.selected ? [
227+
styles.defaultSelectedTitle,
228+
item.props.selectedTitleStyle,
229+
] : null,
230+
]}
231+
badge={badge}
232+
onPress={item.props.onPress}
233+
hidesTabTouch={this.props.hidesTabTouch}>
234+
{icon}
235+
</Tab>
236+
);
237+
}.bind(this);
238+
239+
let { style, children, tabBarStyle, tabBarShadowStyle, sceneStyle, ...props } = this.props;
162240
return (
163-
<Navigator
164-
initialRoute={this.props.firstRoute}
165-
navigationBar={navigationBar}
166-
renderScene={this.renderScene}
167-
onDidFocus={this.onDidFocus}
168-
/>
241+
<View style={styles.container}>
242+
<Navigator
243+
initialRoute={this.props.firstRoute}
244+
navigationBar={navigationBar}
245+
renderScene={this.renderScene}
246+
onDidFocus={this.onDidFocus}
247+
/>
248+
<TabBar style={tabBarStyle} shadowStyle={tabBarShadowStyle}>
249+
{React.Children.map(children, _renderTab)}
250+
</TabBar>
251+
</View>
169252
)
170-
}
253+
};
171254
}
172255

173256
var styles = StyleSheet.create({
174257
container: {
175258
flex: 1,
176259
backgroundColor: '#F5FCFF',
177-
marginTop: 64
178260
},
261+
tabsContainer: {
262+
backgroundColor: '#f8f8f8',
263+
flexDirection: 'row',
264+
justifyContent: 'space-around',
265+
height: Layout.tabBarHeight,
266+
position: 'absolute',
267+
bottom: 0,
268+
left: 0,
269+
right: 0,
270+
},
271+
shadow: {
272+
backgroundColor: 'rgba(0, 0, 0, 0.25)',
273+
height: Layout.pixel,
274+
position: 'absolute',
275+
left: 0,
276+
right: 0,
277+
top: Platform.OS === 'android' ? 0 : -Layout.pixel,
278+
},
279+
179280
});
281+
282+
Nav.Tabitem = Tabitem;

src/TabBar.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/TabItem.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/badge.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/components/Badge.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
import React from 'react-native';
4+
let {
5+
StyleSheet,
6+
Text,
7+
} = React;
8+
9+
import Layout from './Layout';
10+
11+
export default class Badge extends React.Component {
12+
static propTypes = Text.propTypes;
13+
14+
constructor(props, context) {
15+
super(props, context);
16+
17+
this._handleLayout = this._handleLayout.bind(this);
18+
}
19+
20+
state = {
21+
computedSize: null,
22+
};
23+
24+
render() {
25+
let { computedSize } = this.state;
26+
let style = {};
27+
if (!computedSize) {
28+
style.opacity = 0;
29+
} else {
30+
style.width = Math.max(computedSize.height, computedSize.width);
31+
}
32+
33+
return (
34+
<Text
35+
{...this.props}
36+
numberOfLines={1}
37+
onLayout={this._handleLayout}
38+
style={[styles.container, this.props.style, style]}>
39+
{this.props.children}
40+
</Text>
41+
);
42+
}
43+
44+
_handleLayout(event) {
45+
let { width, height } = event.nativeEvent.layout;
46+
let { computedSize } = this.state;
47+
if (computedSize && computedSize.height === height &&
48+
computedSize.width === width) {
49+
return;
50+
}
51+
52+
this.setState({
53+
computedSize: { width, height },
54+
});
55+
56+
if (this.props.onLayout) {
57+
this.props.onLayout(event);
58+
}
59+
}
60+
}
61+
62+
let styles = StyleSheet.create({
63+
container: {
64+
fontSize: 12,
65+
color: '#fff',
66+
backgroundColor: 'rgb(0, 122, 255)',
67+
lineHeight: 15,
68+
textAlign: 'center',
69+
borderWidth: 1 + Layout.pixel,
70+
borderColor: '#fefefe',
71+
borderRadius: 17 / 2,
72+
overflow: 'hidden',
73+
},
74+
});
File renamed without changes.

src/components/NavBarContainer.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import React, {
44
StyleSheet,
55
View,
6+
Text,
67
Component,
78
} from 'react-native';
89

910

1011
import NavBarContent from './NavBarContent';
1112

13+
1214
export default class NavBarContainer extends Component {
1315

1416
constructor (props) {
@@ -46,26 +48,27 @@ export default class NavBarContainer extends Component {
4648
// We render both the current and the previous navbar (for animation)
4749
render () {
4850
return (
49-
<View style={[styles.navbarContainer]}>
50-
<NavBarContent
51-
route={this.state.previousRoute}
52-
backButtonComponent={this.props.backButtonComponent}
53-
rightCorner={this.props.rightCorner}
54-
headerStyle={this.props.style}
55-
titleStyle={this.props.titleStyle}
56-
willDisappear="true"
57-
/>
58-
<NavBarContent
59-
route={this.props.currentRoute}
60-
backButtonComponent={this.props.backButtonComponent}
61-
rightCorner={this.props.rightCorner}
62-
headerStyle={this.props.style}
63-
titleStyle={this.props.titleStyle}
64-
goBack={this.goBack}
65-
goForward={this.goForward}
66-
customAction={this.customAction}
67-
/>
68-
</View>
51+
<View style={[styles.navbarContainer]}>
52+
<NavBarContent
53+
route={this.state.previousRoute}
54+
backButtonComponent={this.props.backButtonComponent}
55+
rightCorner={this.props.rightCorner}
56+
headerStyle={this.props.style}
57+
titleStyle={this.props.titleStyle}
58+
willDisappear="true"
59+
/>
60+
<NavBarContent
61+
route={this.props.currentRoute}
62+
backButtonComponent={this.props.backButtonComponent}
63+
rightCorner={this.props.rightCorner}
64+
headerStyle={this.props.style}
65+
titleStyle={this.props.titleStyle}
66+
goBack={this.goBack}
67+
goForward={this.goForward}
68+
customAction={this.customAction}
69+
/>
70+
</View>
71+
6972
)
7073
}
7174

@@ -74,6 +77,7 @@ export default class NavBarContainer extends Component {
7477

7578

7679
var styles = StyleSheet.create({
80+
7781
navbarContainer: {
7882
position: 'absolute',
7983
top: 0,

src/components/NavBarContent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ var NavBarContent = React.createClass({
117117
<View>
118118
{titleContent}
119119
</View>
120+
120121
);
121122

122123

@@ -168,7 +169,8 @@ var styles = StyleSheet.create({
168169
},
169170
buttonTextRight: {
170171
marginRight: 10
171-
}
172+
},
173+
172174
});
173175

174176
export default NavBarContent;

0 commit comments

Comments
 (0)