换个方式再爱你 發表於 2019-6-27 00:00:00

react-navigation 的抽屉效果 createDrawerNavigator (DrawerNavigator)

<p><span style="font-size: 15px">&nbsp;</span></p>
<h3>一、前言:&nbsp;</h3>
<p><span style="font-size: 15px">&nbsp;  react-navigation&nbsp; 3.x 版本中, 使用<strong>createDrawerNavigator</strong> 替换 原先的DrawerNavigator 方法;</span></p>
<p><span style="font-size: 15px">  那么,当前createBottomTabNavigator、createStackNAvigator、createDrawerNavigator、createSwitchNavigator四兄弟齐全;</span></p>
<p><span style="font-size: 15px">  无论使用哪一个,或者使用一个包含另一个,他们都有最终的“爸爸”——createAppContainer。</span></p>
<p><span style="font-size: 15px">&nbsp;</span></p>
<h3>二、react-navigation 延伸</h3>
<ul>
<li><span style="font-size: 15px">createStackNAvigator —— StackActions</span></li>
<li><span style="font-size: 15px">createDrawerNavigator —— DrawerActions</span></li>
<li><span style="font-size: 15px">createSwitchNavigator —— SwitchActions</span></li>
</ul>
<p><span style="font-size: 15px">  StackActions、DrawerActions、SwitchActions都是对应create方法的;</span></p>
<p><span style="font-size: 15px">  而且都三者都扩展了 NavigationActions 中的方法</span></p>
<p><span style="font-size: 15px">&nbsp;</span></p>
<h3><span style="font-size: 15px">三、使用分析</span></h3>
<div class="cnblogs_code">
<pre><span style="font-size: 15px">const MainDrawer =<span style="color: rgba(0, 0, 0, 1)"> createDrawerNavigator({
    MainStack: MainStack,
},{
    order: [</span>'MainStack'<span style="color: rgba(0, 0, 0, 1)">],
    initialRouteName: </span>'MainStack'<span style="color: rgba(0, 0, 0, 1)">,
    drawerLockMode: </span>'locked-closed'<span style="color: rgba(0, 0, 0, 1)">,
    drawerWidth: width</span>*0.75<span style="color: rgba(0, 0, 0, 1)">,
    drawerPosition: </span>'left'<span style="color: rgba(0, 0, 0, 1)">,
    useNativeAnimations: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    overlayColor: </span>'rgba(0,0,0,0.6)'<span style="color: rgba(0, 0, 0, 1)">,
    contentComponent: (props) </span>=&gt; &lt;CustomDrawerComponent {...props} /&gt;,
<span style="color: rgba(0, 0, 0, 1)">    contentOptions: {
      activeTintColor: </span>'#fb7299'<span style="color: rgba(0, 0, 0, 1)">,
      activeBackgroundColor: </span>'#ccc'<span style="color: rgba(0, 0, 0, 1)">,
      inactiveTintColor: </span>'balck'<span style="color: rgba(0, 0, 0, 1)">,
    }
})</span></span></pre>
</div>
<ul>
<li><span style="font-size: 15px"><code>initialRouteName</code>&nbsp;-第一次加载时初始选项卡路由的 routeName。</span></li>
<li><span style="font-size: 15px"><code>order</code>&nbsp;-定义选项卡顺序的 routeNames 数组。</span></li>
</ul>
<ul>
<li><span style="font-size: 15px"><code>drawerWidth</code>&nbsp;- 定义抽屉的宽度,一般使用屏宽的百分比。</span></li>
<li><span style="font-size: 15px"><code>drawerPosition</code>&nbsp;- 可选值:&nbsp;<code>left</code>&nbsp;或&nbsp;<code>right</code>, 默认值:&nbsp;<code>left</code>。</span></li>
<li><span style="font-size: 15px"><code>contentComponent</code>&nbsp;-用于呈现抽屉内容 (例如, 导航项) 的组件。可以完全使用自定义组件 。</span></li>
<li><span style="font-size: 15px"><code>useNativeAnimations</code>&nbsp;- 使用原生动画, 默认值:&nbsp;<code>true</code>。</span></li>
<li><span style="font-size: 15px"><code>drawerBackgroundColor</code>&nbsp;- 使用抽屉背景色, 默认值:<code>white</code>。</span></li>
<li><span style="font-size: 15px"><code>contentOptions</code>&nbsp;-配置抽屉内容, 请参阅下面。</span></li>
<ul>
<li><span style="font-size: 15px"><code>items</code>&nbsp;- 路由数组,可以修改或覆盖</span></li>
<li><span style="font-size: 15px"><code>activeItemKey</code>&nbsp;- 识别活动路线的关键字</span></li>
<li><span style="font-size: 15px"><code>activeTintColor</code>&nbsp;-活动选项卡的标签和图标颜色。</span></li>
<li><span style="font-size: 15px"><code>activeBackgroundColor</code>&nbsp;-活动选项卡的背景色。</span></li>
<li><span style="font-size: 15px"><code>inactiveTintColor</code>&nbsp;-"非活动" 选项卡的标签和图标颜色。</span></li>
<li><span style="font-size: 15px"><code>inactiveBackgroundColor</code>&nbsp;-非活动选项卡的背景色。</span></li>
<li><span style="font-size: 15px"><code>onItemPress (路由)</code>&nbsp;-按下某项时调用的函数</span></li>
<li><span style="font-size: 15px"><code>itemsContainerStyle</code>&nbsp;-内容节的样式对象</span></li>
<li><span style="font-size: 15px"><code>itemStyle</code>&nbsp;样式对象的单个项, 可以包含图标和/或标签</span></li>
<li><span style="font-size: 15px"><code>labelStyle</code>&nbsp;样式对象要覆盖&nbsp;<code>文本</code>&nbsp;样式内部内容部分, 当您的标签是一个字符串</span></li>
<li><span style="font-size: 15px"><code>activeLabelStyle</code>&nbsp;样式对象若要改写活动标签的&nbsp;<code>文本</code>&nbsp;样式, 则标签为字符串 (合并使用&nbsp;<code>labelStyle</code>)</span></li>
<li><span style="font-size: 15px"><code>inactiveLabelStyle</code>&nbsp;样式对象在标签为字符串时覆盖&nbsp;<code>文本</code>&nbsp;样式的非活动标签 (与&nbsp;<code>labelStyle</code>&nbsp;合并)</span></li>
<li><span style="font-size: 15px"><code>iconContainerStyle</code>&nbsp;- 用于覆盖<code>View</code>图标容器样式的样式对象。</span></li>
</ul>
<li><span style="font-size: 15px"><code>overlayColor</code>&nbsp;- 可以修改抽屉剩余部分的背景色。</span></li>
<li><span style="font-size: 15px">drawerLockMode -&nbsp;指定抽屉的锁定模式,<code class="php"><code class="php"><code class="php"><span class="hljs-string">'unlocked',&nbsp;<code class="php"><span class="hljs-comment">'locked-closed,&nbsp;<code class="php"><span class="hljs-comment">'locked-open'。</span></code></span></code></span></code></code></code></span></li>
</ul>
<p><span style="font-size: 15px"><code class="php"><code class="php"><code class="php"><span class="hljs-string"><code class="php"><span class="hljs-comment"><br></span></code></span></code></code></code></span></p>
<h3>四,注意点</h3>
<p><span style="font-size: 15px">1.&nbsp;<code>useNativeAnimations</code>&nbsp;需要设置为 true,否则抽屉的动画会很生涩;</span></p>
<p><span style="font-size: 15px">2.&nbsp;<code>overlayColor</code>&nbsp;可以修改抽屉剩余部分的颜色,因为自带透明度,所以仅仅修改颜色不能修改透明的,但是我们可以通过 rgba(0,0,0,0.x) 来达到效果</span></p>
<p><span style="font-size: 15px">3.&nbsp;drawerLockMode -&nbsp;指定抽屉的锁定模式,</span></p>
<ul>
<li><span style="font-size: 15px"><code class="php"><code class="php"><code class="php"><span class="hljs-string">'unlocked', 表示无锁状态,可以通过手势或代码,打开关闭抽屉&nbsp;</span></code></code></code></span></li>
<li><span style="font-size: 15px"><code class="php"><code class="php"><code class="php"><span class="hljs-string"><code class="php"><span class="hljs-comment">'locked-closed, 表示抽屉是关闭状态时,不能通过手势打开,只能通过代码</span></code></span></code></code></code></span></li>
<li><span style="font-size: 15px"><code class="php"><code class="php"><code class="php"><span class="hljs-string"><code class="php"><span class="hljs-comment"><code class="php"><span class="hljs-comment">'locked-open',表示抽屉是打开状态时,不能通过手势关闭,只能通过代码</span></code></span></code></span></code></code></code></span></li>
</ul>
<p><span style="font-size: 15px">&nbsp;4. 代码 打开和关闭抽屉的方法:</span></p>
<ul>
<li><span style="font-size: 15px">this.props.navigation.openDrawer(); 可以打开抽屉</span></li>
<li><span style="font-size: 15px">this.props.navigation.closeDrawer(); 可以关闭抽屉</span></li>
<li><span style="font-size: 15px">this.props.navigation.toggleDrawer(); 可以打开/关闭抽屉</span></li>
</ul>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/nangezi/p/11091354.html
頁: [1]
查看完整版本: react-navigation 的抽屉效果 createDrawerNavigator (DrawerNavigator)