iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 5
0
Modern Web

我不用Expo啦,React Native!系列 第 5

[Day5] 在導覽列加入圖示

  • 分享至 

  • xImage
  •  

今日關鍵字:vector icon


接續昨天
參考Tab navigation的Customizing the appearance

import Ionicons from 'react-native-vector-icons/Ionicons';

// (...)

export default function App() {
  return (
    <NavigationContainer>
      <Tab.Navigator
        screenOptions={({ route }) => ({
          tabBarIcon: ({ focused, color, size }) => {
            let iconName;

            if (route.name === 'Home') {
              iconName = focused
                ? 'ios-information-circle'
                : 'ios-information-circle-outline';
            } else if (route.name === 'Settings') {
              iconName = focused ? 'ios-list-box' : 'ios-list';
            }

            // You can return any component that you like here!
            return <Ionicons name={iconName} size={size} color={color} />;
          },
        })}
        tabBarOptions={{
          activeTintColor: 'tomato',
          inactiveTintColor: 'gray',
        }}
      >
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}

DEMO看起來還不錯,直接來使用吧
https://ithelp.ithome.com.tw/upload/images/20200905/20121828NjaDFN0OLR.png


首先要先安裝react-native-vector-icons

yarn add react-native-vector-icons @types/react-native-vector-icons

cd ios && pod install && cd ..

有些套件在使用ts時還需安裝@types/~

Android

react-native link  react-native-vector-icons  --platforms android

這個是連接原生庫的動作,雖然很多都自動化了,不過有些還是需要這樣連接
(不過以後就沒有這個指令,要完全自動化了)

IOS

如同github說的,我直接使用react-native link安裝有問題
只好手動來了

打開Xcode從專案按右鍵,選擇加入檔案
https://ithelp.ithome.com.tw/upload/images/20200905/20121828t5fCgrTYED.png

加入node_modules/react-native-vector-icons下的Fonts資料夾
https://ithelp.ithome.com.tw/upload/images/20200905/20121828Pj9TcbmiEJ.png

並在專案中的專案名資料夾下的info.plist加入Fonts provided by application並key入剛剛加入的檔案名
https://ithelp.ithome.com.tw/upload/images/20200905/20121828mxQpk5xZwC.png
https://ithelp.ithome.com.tw/upload/images/20200905/201218282fkl3Jut2b.png
(這裡只是示範,當然可以裝要用的就好)

接著套入範例到昨天的部分

...
import Ionicons from 'react-native-vector-icons/Ionicons';
...
const App = () => {
  return (
    <NavigationContainer>
      {/* <Tab.Navigator>
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator> */}

      <Tab.Navigator
        screenOptions={({route}) => ({
          tabBarIcon: ({focused, color, size}) => {
            let iconName;

            if (route.name === 'Home') {
              iconName = focused
                ? 'ios-information-circle'
                : 'ios-information-circle-outline';
            } else if (route.name === 'Settings') {
              iconName = focused ? 'ios-list-box' : 'ios-list';
            }

            // You can return any component that you like here!
            return <Ionicons name={iconName} size={size} color={color} />;
          },
        })}
        tabBarOptions={{
          activeTintColor: 'tomato',
          inactiveTintColor: 'gray',
        }}>
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

然後啟動看看

yarn ios

https://ithelp.ithome.com.tw/upload/images/20200905/20121828gswgYXJBvf.png
黃字警告/images/emoticon/emoticon06.gif

這時來找尋看看
ios-list-box輸入react native各種向量圖的搜尋網站
https://ithelp.ithome.com.tw/upload/images/20200905/20121828R87KbRST5o.png
搞...什麼啊?這不是官方範例嗎/images/emoticon/emoticon03.gif
先改回ios-list就成功了
https://ithelp.ithome.com.tw/upload/images/20200905/2012182867WuvFRNOs.png

android這邊沒什麼問題
https://ithelp.ithome.com.tw/upload/images/20200905/20121828B6iQV5jFp2.png


現在來將App.tsx整理一下

先如同這樣把要做的四個畫面拆出來

import React from 'react';
import {View, Text} from 'react-native';

const SettingsScreen = () => {
  return (
    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
      <Text>Settings!</Text>
    </View>
  );
};

export default SettingsScreen;

再建立一個TabNavigation.tsx
搬入Navigation

...
import HomeScreen from '../components/screen/HomeScreen';
import SearchingScreen from '../components/screen/SearchingScreen';
import FavoriteScreen from '../components/screen/FavoriteScreen';
import SettingsScreen from '../components/screen/SettingsScreen';

const Tab = createBottomTabNavigator();

const TabNavigation = () => {
  return (
    <NavigationContainer>
      <Tab.Navigator
        screenOptions={({route}) => ({
          tabBarIcon: ({focused, color, size}) => {
            let iconName;

            if (route.name === 'Home') {
              iconName = focused
                ? 'ios-information-circle'
                : 'ios-information-circle-outline';
            } else if (route.name === 'Settings') {
              iconName = focused ? 'ios-list' : 'ios-list';
            }

            // You can return any component that you like here!
            return <Ionicons name={iconName} size={size} color={color} />;
          },
        })}
        tabBarOptions={{
          activeTintColor: 'tomato',
          inactiveTintColor: 'gray',
        }}>
        <Tab.Screen name="Home" component={HomeScreen} />
        <Tab.Screen name="Searching" component={SearchingScreen} />
        <Tab.Screen name="Favorite" component={FavoriteScreen} />
        <Tab.Screen name="Settings" component={SettingsScreen} />
      </Tab.Navigator>
    </NavigationContainer>
  );
};

export default TabNavigation;

現在App.tsx非常乾淨

import React from 'react';

import TabNavigation from './src/components/TabNavigation';

const App = () => <TabNavigation />;

export default App;

資料夾結構
https://ithelp.ithome.com.tw/upload/images/20200905/20121828s7MeeZjtVk.png

現在畫面是這樣
https://ithelp.ithome.com.tw/upload/images/20200905/20121828AndhDn1WMW.png


明天來講點TypeScript順便完成icon設定
再來談談資料格式跟如何清除現有的eslint的紅字

參考:react-native-vector-icons Github


上一篇
[Day4] 加入導覽列-Android:你再冷落我試試看啊?
下一篇
[Day6] 訂定資料格式-TypeScript:型別判斷
系列文
我不用Expo啦,React Native!33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言