{"id":526,"date":"2023-02-25T15:48:16","date_gmt":"2023-02-25T07:48:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=526"},"modified":"2023-04-29T20:42:27","modified_gmt":"2023-04-29T12:42:27","slug":"react-native-universal-tab-component-react-native-scrollable-tab-view-custom-tabbar","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/react-native-universal-tab-component-react-native-scrollable-tab-view-custom-tabbar\/","title":{"rendered":"React Native\u901a\u7528Tab\u7ec4\u4ef6react-native-scrollable-tab-view\u81ea\u5b9a\u4e49TabBar"},"content":{"rendered":"<p>\u5b98\u65b9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u79cd\u57fa\u672c\u7684Tab\u63a7\u5236\u5668\u6837\u5f0f\uff0c<code>DefaultTabBar<\/code>\u548c<code>ScrollableTabBar<\/code>\u3002\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u5b98\u65b9\u7684\u6837\u5f0f\u5e76\u4e0d\u80fd\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\uff08\u5907\u6ce8\uff1a\u5b98\u65b9\u7684\u6837\u5f0f\u662f\u6587\u5b57+\u4e0b\u5212\u7ebf\u7684\u98ce\u683c\uff09\uff0c\u90a3\u4e48\u6b64\u65f6\u5c31\u9700\u8981\u6211\u4eec\u81ea\u5df1\u6765\u5b9e\u73b0\u7279\u5b9a\u7684\u6837\u5f0f\u3002<\/p>\n<p>\u5f00\u6e90\u9879\u76ee\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/skv-headless\/react-native-scrollable-tab-view\">https:\/\/github.com\/skv-headless\/react-native-scrollable-tab-view<\/a><\/p>\n<p><!-- more --><\/p>\n<p>\u5e38\u7528\u7684\u5c31\u662f\uff1a\u6587\u5b57+\u56fe\u6807\uff08\u5e26\u989c\u8272\u9009\u62e9\u5668\uff09<\/p>\n<h2>react-native-vector-icons \u56fe\u6807\u5e93<\/h2>\n<p><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/oblador\/react-native-vector-icons\">https:\/\/github.com\/oblador\/react-native-vector-icons<\/a><\/p>\n<p>\u6dfb\u52a0<code>react-native-vector-icons<\/code>\u4e00\u4e2a\u201c\u56fe\u6807\u201d\u5e93\uff0c\u5b98\u65b9\u63cf\u8ff0\u4e3a\u20183000 Customizable Icons for React Native with support for NavBar\/TabBar\/ToolbarAndroid, image source and full stying.\u2019 \u53ef\u89c1\uff0c\u8fd9\u4e2a\u5e93\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5f88\u591a\u56fe\u6807\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u82b1\u8d39\u65f6\u95f4\u53bb\u8bbe\u8ba1\u4e00\u4e9b\u56fe\u6807\uff0c\u4e0d\u59a8\u4f7f\u7528\u8fd9\u4e2a\u5e93\u6765\u66ff\u4ee3\u3002<\/p>\n<p>\u5b89\u88c5\u7ec4\u4ef6<\/p>\n<pre><code>npm install react-native-vector-icons --save<\/code><\/pre>\n<p>\u5b89\u88c5\u4f9d\u8d56<\/p>\n<pre><code>rnpm link<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\uff1a\u8be5\u547d\u4ee4\u9700\u8981\u91cd\u65b0\u7f16\u8bd1\u9879\u76ee\uff0c\u5e76\u4e14\u9996\u5148\u9700\u8981\u5b89\u88c5 rmpm<\/p>\n<\/blockquote>\n<pre><code>npm install rnpm -g<\/code><\/pre>\n<p>\u8fd9\u4e2a\u5e93\u7684\u56fe\u6807\u6765\u6e90\u6709\u5f88\u591a\uff0c\u5176\u4e2d\u7528\u7684\u6700\u591a\u7684\u662f\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/ionicframework.com\/docs\/v2\/ionicons\/\">http:\/\/ionicframework.com\/docs\/v2\/ionicons\/<\/a><\/p>\n<p>\u4f7f\u7528\u7ec4\u4ef6<\/p>\n<pre><code class=\"language-javascript\">import Icon from &#039;react-native-vector-icons\/Ionicons&#039;;\nimport IconFont from &#039;react-native-vector-icons\/FontAwesome&#039;;<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;Icon name=&quot;md-alarm&quot; size={20}&gt;&lt;\/Icon&gt;\n&lt;IconFont.Button\n    name=&quot;facebook&quot;\n    backgroundColor=&quot;#3b5998&quot;\n    size={20}\n    &gt;\n    Login with Facebook\n&lt;\/IconFont.Button&gt;<\/code><\/pre>\n<h2>\u81ea\u5b9a\u4e49TabBar\u7684\u6b65\u9aa4<\/h2>\n<h3>\u6dfb\u52a0\u5fc5\u987b\u7684\u5c5e\u6027\u7c7b\u578b\uff083\u4e2a\uff09<\/h3>\n<pre><code class=\"language-javascript\">static propTypes = {\n    goToPage: React.PropTypes.func, \/\/ \u8df3\u8f6c\u5230\u5bf9\u5e94tab\u7684\u65b9\u6cd5\n    activeTab: React.PropTypes.number, \/\/ \u5f53\u524d\u88ab\u9009\u4e2d\u7684tab\u4e0b\u6807\n    tabs: React.PropTypes.array, \/\/ \u6240\u6709tabs\u96c6\u5408\n};  \/\/ \u6ce8\u610f\u8fd9\u91cc\u6709\u5206\u53f7<\/code><\/pre>\n<h3>\u5982\u679c\u9700\u8981\u5728\u6ed1\u52a8\u65f6\u7684\u52a8\u753b\uff0c\u5219<\/h3>\n<pre><code class=\"language-javascript\">componentDidMount() {\n    \/\/ Animated.Value\u76d1\u542c\u8303\u56f4 [0, tab\u6570\u91cf-1]\n    this.props.scrollValue.addListener(this.setAnimationValue);\n}\n\nsetAnimationValue({value}) {\n    console.log(&#039;\u52a8\u753b\u503c\uff1a&#039; + value);\n}<\/code><\/pre>\n<h3>render\u65b9\u6cd5\u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u7ec4\u4ef6\u4f5c\u4e3aTabBar<\/h3>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5373\u4f7f\u6211\u4eec\u4e0d\u4f7f\u7528\u7cfb\u7edf\u7684DefaultTabBar\u548cScrollableTabBar\uff0c\u4f46tabLabel\u8fd9\u4e2a\u5c5e\u6027\u5fc5\u987b\u4f7f\u7528\uff0c\u4e14\u503c\u4e0d\u80fd\u91cd\u590d\u3002<\/p>\n<\/blockquote>\n<h2>\u8303\u4f8b\u6e90\u7801<\/h2>\n<p>index.android.js &amp; index.ios.js<\/p>\n<pre><code class=\"language-javascript\">\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n * @flow\n *\/\n\nimport React,{Component} from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    View\n} from &#039;react-native&#039;;\n\nimport ScrollableTabView, {DefaultTabBar, ScrollableTabBar} from &#039;react-native-scrollable-tab-view&#039;;\n\nimport Icon from &#039;react-native-vector-icons\/Ionicons&#039;;\nimport IconFont from &#039;react-native-vector-icons\/FontAwesome&#039;;\n\nimport MyTabBar from &#039;.\/MyTabBar&#039;;\n\nclass RNAPP extends Component {\n    constructor(props) {\n        super(props);\n        this.state = {\n            tabNames: [&#039;\u4e3b\u9875&#039;, &#039;\u5206\u7c7b&#039;, &#039;\u79d2\u6740&#039;, &#039;\u8d2d\u7269\u8f66&#039;, &#039;\u6211\u7684&#039;],\n            tabIconNames: [&#039;md-home&#039;, &#039;md-grid&#039;, &#039;md-time&#039;, &#039;md-cart&#039;, &#039;md-contact&#039;],\n        };\n    }\n\n    \/\/ tabIconNames: [&#039;ios-home&#039;, &#039;ios-grid&#039;, &#039;ios-time&#039;, &#039;ios-cart&#039;, &#039;ios-contact&#039;],\n\n    render() {\n        let tabNames = this.state.tabNames;\n        let tabIconNames = this.state.tabIconNames;\n        return (\n            &lt;ScrollableTabView\n                \/\/ renderTabBar={() =&gt; &lt;ScrollableTabBar\/&gt;}\n                renderTabBar={() =&gt; &lt;MyTabBar tabNames={tabNames} tabIconNames={tabIconNames}\/&gt;}\n                tabBarPosition=&#039;bottom&#039;\n                locked={false}\n                initialPage={0}\n                prerenderingSiblingsNumber={1}\n\n                onChangeTab={\n                    (obj) =&gt; {\n                        console.log(&#039;\u88ab\u9009\u4e2d\u7684tab\u4e0b\u6807\uff1a&#039; + obj.i);\n                    }\n                }\n\n                onScroll={\n                    (position) =&gt; {\n                        console.log(&#039;\u6ed1\u52a8\u65f6\u7684\u4f4d\u7f6e\uff1a&#039; + position);\n                    }\n                }\n                &gt;\n\n                &lt;View tabLabel=&quot;RNAPP.CC&quot; style={styles.center}&gt;\n                    &lt;Text style={{marginBottom:10}} &gt;\u5185\u5bb9\uff1aReactNative&lt;\/Text&gt;\n                    &lt;IconFont.Button name=&quot;facebook&quot; backgroundColor=&quot;#3b5998&quot; size={20} &gt;\n                        Login with Facebook\n                    &lt;\/IconFont.Button&gt;\n                    &lt;Icon\n                        name=&quot;md-alarm&quot;\n                        size={50}\n                        style={{marginTop:10, marginBottom:10}}\n                        \/&gt;\n                    &lt;IconFont.Button name=&quot;twitter&quot; backgroundColor=&quot;#3b5998&quot; size={20} &gt;\n                        Follow me on Twitter\n                    &lt;\/IconFont.Button&gt;\n                    &lt;Text style={styles.author}&gt;\n                        Powered by RNAPP.CC\n                    &lt;\/Text&gt;\n                &lt;\/View&gt;\n\n                &lt;View tabLabel=&quot;ReactNative&quot; style={styles.center}&gt;\n                    &lt;Text &gt;\u5185\u5bb9\uff1aReact Native&lt;\/Text&gt;\n                &lt;\/View&gt;\n\n                &lt;View tabLabel=&quot;Android&quot; style={styles.center}&gt;\n                    &lt;Text &gt;\u5185\u5bb9\uff1aAndroid&lt;\/Text&gt;\n                &lt;\/View&gt;\n\n                &lt;View tabLabel=&quot;iOS&quot; style={styles.center}&gt;\n                    &lt;Text &gt;\u5185\u5bb9\uff1aiOS&lt;\/Text&gt;\n                &lt;\/View&gt;\n\n                &lt;View tabLabel=&quot;APP\u5f00\u53d1&quot; style={styles.center}&gt;\n                    &lt;Text &gt;\u5185\u5bb9\uff1aAPP\u5f00\u53d1&lt;\/Text&gt;\n                &lt;\/View&gt;\n            &lt;\/ScrollableTabView&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    container: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        alignItems: &#039;center&#039;,\n        backgroundColor: &#039;#F5FCFF&#039;,\n    },\n    center: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        alignItems: &#039;center&#039;,\n    },\n    welcome: {\n        fontSize: 20,\n        textAlign: &#039;center&#039;,\n        margin: 10,\n    },\n    instructions: {\n        textAlign: &#039;center&#039;,\n        color: &#039;#333333&#039;,\n        marginBottom: 5,\n    },\n    author: {\n        textAlign: &#039;center&#039;,\n        color: &#039;#3BC1FF&#039;,\n        marginTop: 20,\n    },\n});\n\nAppRegistry.registerComponent(&#039;RNAPP&#039;, () =&gt; RNAPP);<\/code><\/pre>\n<p>MyTabBar.js<\/p>\n<pre><code>\/**\n * Sample React Native App\n * https:\/\/github.com\/facebook\/react-native\n * @flow\n *\/\n\nimport React, { Component } from &#039;react&#039;;\nimport {\n    AppRegistry,\n    StyleSheet,\n    Text,\n    TouchableOpacity,\n    View\n} from &#039;react-native&#039;;\n\nimport Icon from &#039;react-native-vector-icons\/Ionicons&#039;;\n\nexport default class MyTabBar extends Component {\n    static propTypes = {\n        goToPage: React.PropTypes.func, \/\/ \u8df3\u8f6c\u5230\u5bf9\u5e94tab\u7684\u65b9\u6cd5\n        activeTab: React.PropTypes.number, \/\/ \u5f53\u524d\u88ab\u9009\u4e2d\u7684tab\u4e0b\u6807\n        tabs: React.PropTypes.array, \/\/ \u6240\u6709tabs\u96c6\u5408\n\n        tabNames: React.PropTypes.array, \/\/ \u4fdd\u5b58Tab\u540d\u79f0\n        tabIconNames: React.PropTypes.array, \/\/ \u4fdd\u5b58Tab\u56fe\u6807\n    };  \/\/ \u6ce8\u610f\u8fd9\u91cc\u6709\u5206\u53f7\n\n    render() {\n        return (\n            &lt;View style={styles.tabs}&gt;\n                {this.props.tabs.map((tab, i) =&gt; this.renderTabOption(tab, i))}\n            &lt;\/View&gt;\n        );\n    }\n\n    componentDidMount() {\n        \/\/ Animated.Value\u76d1\u542c\u8303\u56f4 [0, tab\u6570\u91cf-1]\n        this.props.scrollValue.addListener(this.setAnimationValue);\n    }\n\n    setAnimationValue({value}) {\n        console.log(&#039;\u52a8\u753b\u503c\uff1a&#039;+value);\n    }\n\n    renderTabOption(tab, i) {\n        let color = this.props.activeTab == i ? &quot;#6B8E23&quot; : &quot;#ADADAD&quot;; \/\/ \u5224\u65adi\u662f\u5426\u662f\u5f53\u524d\u9009\u4e2d\u7684tab\uff0c\u8bbe\u7f6e\u4e0d\u540c\u7684\u989c\u8272\n        return (\n            &lt;TouchableOpacity onPress={()=&gt;this.props.goToPage(i)} style={styles.tab} key={tab}&gt;\n                &lt;View style={styles.tabItem}&gt;\n                    &lt;Icon\n                        name={this.props.tabIconNames[i]} \/\/ \u56fe\u6807\n                        size={30}\n                        color={color}\n                        \/&gt;\n                    &lt;Text style={{color: color}}&gt;\n                        {this.props.tabNames[i]}\n                    &lt;\/Text&gt;\n                &lt;\/View&gt;\n            &lt;\/TouchableOpacity&gt;\n        );\n    }\n}\n\nconst styles = StyleSheet.create({\n    tabs: {\n        flexDirection: &#039;row&#039;,\n        height: 50,\n    },\n    tab: {\n        flex: 1,\n        justifyContent: &#039;center&#039;,\n        alignItems: &#039;center&#039;,\n    },\n    tabItem: {\n        flexDirection: &#039;column&#039;,\n        alignItems: &#039;center&#039;,\n    },\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5b98\u65b9\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u79cd\u57fa\u672c\u7684Tab\u63a7\u5236\u5668\u6837\u5f0f\uff0cDefaultTabBar\u548cScrollableTabBar\u3002\u5f88\u591a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-526","post","type-post","status-publish","format-standard","hentry","category-react-native"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=526"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/526\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=526"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=526"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}