Platform
示例
参考
属性
constants
tsx
static constants: PlatformConstants;
返回一个对象,其中包含与平台相关的所有可用通用和特定常量。
属性
姓名 | 类型 | 可选 | 描述 |
|---|---|---|---|
| isTesting | 布尔值 | 否 | |
| reactNativeVersion | 对象 | 否 | 关于 React Native 版本的信息。键是 major、minor、patch,以及可选的 prerelease,值都是 number。 |
| Version Android | 数字 | 否 | Android 专用的操作系统版本常量。 |
| Release Android | 字符串 | 否 | |
| Serial Android | 字符串 | 否 | Android 设备的硬件序列号。 |
| Fingerprint Android | 字符串 | 否 | 唯一标识构建的字符串。 |
| Model Android | 字符串 | 否 | Android 设备最终用户可见的名称。 |
| Brand Android | 字符串 | 否 | 与产品/硬件关联的消费者可见的品牌。 |
| Manufacturer Android | 字符串 | 否 | Android 设备的制造商。 |
| ServerHost Android | 字符串 | 是 | |
| uiMode Android | 字符串 | 否 | 可能的值有:'car'、'desk'、'normal'、'tv'、'watch' 和 'unknown'。阅读更多关于Android ModeType。 |
| forceTouchAvailable iOS | 布尔值 | 否 | 指示设备上 3D Touch 的可用性。 |
| interfaceIdiom iOS | 字符串 | 否 | 设备的界面类型。阅读更多关于UIUserInterfaceIdiom。 |
| osVersion iOS | 字符串 | 否 | iOS 专用的操作系统版本常量。 |
| systemName iOS | 字符串 | 否 | iOS 专用的操作系统名称常量。 |
isPad iOS
tsx
static isPad: boolean;
返回一个布尔值,定义设备是否为 iPad。
| 类型 |
|---|
| 布尔值 |
isTV
tsx
static isTV: boolean;
返回一个布尔值,定义设备是否为电视。
| 类型 |
|---|
| 布尔值 |
isVision
tsx
static isVision: boolean;
返回一个布尔值,定义设备是否为 Apple Vision。如果您正在使用Apple Vision Pro(专为 iPad 设计),isVision 将为 false,但 isPad 将为 true
| 类型 |
|---|
| 布尔值 |
isTesting
tsx
static isTesting: boolean;
返回一个布尔值,定义应用程序是否以开发人员模式运行,并设置了测试标志。
| 类型 |
|---|
| 布尔值 |
OS
tsx
static OS: 'android' | 'ios';
返回表示当前操作系统的字符串值。
| 类型 |
|---|
枚举 ('android', 'ios') |
Version
tsx
static Version: 'number' | 'string';
返回操作系统的版本。
| 类型 |
|---|
| 数字 Android 字符串 iOS |
方法
select()
tsx
static select(config: Record<string, T>): T;
返回当前运行平台最适合的值。
参数:
| 姓名 | 类型 | 必需 | 描述 |
|---|---|---|---|
| config | 对象 | 是 | 请参阅下面的配置描述。 |
Select 方法返回当前运行平台最适合的值。也就是说,如果您在手机上运行,android 和 ios 键将优先。如果未指定这些键,则将使用 native 键,然后是 default 键。
config 参数是一个对象,包含以下键:
android(任意)ios(任意)native(任意)default(任意)
用法示例
tsx
import {Platform, StyleSheet} from 'react-native';
const styles = StyleSheet.create({
container: {
flex: 1,
...Platform.select({
android: {
backgroundColor: 'green',
},
ios: {
backgroundColor: 'red',
},
default: {
// other platforms, web for example
backgroundColor: 'blue',
},
}),
},
});
这将导致容器在所有平台上都具有 flex: 1,在 Android 上背景色为绿色,在 iOS 上背景色为红色,在其他平台上背景色为蓝色。
由于相应平台键的值可以是 any 类型,因此select 方法也可以用于返回特定于平台的组件,如下所示:
tsx
const Component = Platform.select({
ios: () => require('ComponentIOS'),
android: () => require('ComponentAndroid'),
})();
<Component />;
tsx
const Component = Platform.select({
native: () => require('ComponentForNative'),
default: () => require('ComponentForWeb'),
})();
<Component />;