Reference version

Expo GlassEffect

使用 iOS 原生 UIVisualEffectView 渲染液态玻璃效果的 React 组件。

iOS
tvOS
Included in Expo Go
Bundled version:
~55.0.0

For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.

重要 GlassView 仅在 iOS 26 及以上版本可用。在不支持的平台上,它会回退为普通的 View

使用 UIVisualEffectView 渲染原生 iOS 液态玻璃效果的 React 组件。支持可自定义的玻璃样式和色调颜色。

已知问题

安装

Terminal
npx expo install expo-glass-effect

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

GlassView

GlassView 组件会渲染原生 iOS 玻璃效果。它支持不同的玻璃效果样式,并且可以通过色调颜色进行定制,以满足各种美学需求。

Basic GlassView usage
import { StyleSheet, View, Image } from 'react-native'; import { GlassView } from 'expo-glass-effect'; export default function App() { return ( <View style={styles.container}> <Image style={styles.backgroundImage} source={{ uri: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop', }} /> {/* 基础 Glass View */} <GlassView style={styles.glassView} /> {/* 带清晰样式的 Glass View */} <GlassView style={styles.tintedGlassView} glassEffectStyle="clear" /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, }, backgroundImage: { ...StyleSheet.absoluteFill, width: '100%', height: '100%', }, glassView: { position: 'absolute', top: 100, left: 50, width: 200, height: 100, borderRadius: 12, justifyContent: 'center', alignItems: 'center', padding: 20, }, tintedGlassView: { position: 'absolute', top: 250, left: 50, width: 200, height: 100, borderRadius: 12, justifyContent: 'center', alignItems: 'center', padding: 20, }, text: { fontSize: 16, fontWeight: '600', color: '#000', textAlign: 'center', }, });

GlassContainer

GlassContainer 组件允许你将多个玻璃视图组合成一个统一效果。

GlassContainer example
import { StyleSheet, View, Image } from 'react-native'; import { GlassView, GlassContainer } from 'expo-glass-effect'; export default function GlassContainerDemo() { return ( <View style={styles.container}> <Image style={styles.backgroundImage} source={{ uri: 'https://images.unsplash.com/photo-1547036967-23d11aacaee0?w=400&h=600&fit=crop', }} /> <GlassContainer spacing={10} style={styles.containerStyle}> <GlassView style={styles.glass1} isInteractive /> <GlassView style={styles.glass2} /> <GlassView style={styles.glass3} /> </GlassContainer> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, }, backgroundImage: { ...StyleSheet.absoluteFillObject, width: '100%', height: '100%', }, containerStyle: { position: 'absolute', top: 200, left: 50, width: 250, height: 100, flexDirection: 'row', alignItems: 'center', gap: 5, }, glass1: { width: 60, height: 60, borderRadius: 30, }, glass2: { width: 50, height: 50, borderRadius: 25, }, glass3: { width: 40, height: 40, borderRadius: 20, }, });

玻璃效果样式动画

glassEffectStyle 属性接受一个包含 animateanimationDuration 属性的配置对象,用于在不同玻璃样式之间进行原生过渡动画。这是在不修改 opacity 的情况下淡入/淡出玻璃效果的推荐方式。

Animated glass effect style
import { useState } from 'react'; import { StyleSheet, Text, View, Image, Pressable } from 'react-native'; import { GlassView } from 'expo-glass-effect'; export default function AnimatedGlassStyleExample() { const [visible, setVisible] = useState(true); return ( <View style={styles.container}> <View style={styles.backgroundImage}> <Image style={{ width: 300, height: 200, }} source={{ uri: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop', }} /> <GlassView style={styles.glassView} glassEffectStyle={{ style: visible ? 'clear' : 'none', animate: true, animationDuration: 0.5, }} /> </View> <Pressable style={styles.toggleButton} onPress={() => setVisible(prev => !prev)}> <Text style={styles.toggleButtonText}>{visible ? '隐藏' : '显示'} 玻璃效果</Text> </Pressable> </View> ); } const styles = StyleSheet.create({ container: { height: 300, width: 300, }, backgroundImage: { position: 'absolute', }, glassView: { position: 'absolute', width: 200, height: 120, borderRadius: 12, }, toggleButton: { position: 'absolute', bottom: 100, alignSelf: 'center', backgroundColor: '#007AFF', paddingVertical: 14, paddingHorizontal: 24, borderRadius: 12, }, toggleButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });

opacity 动画变通方案

由于将 GlassView 或其父视图的 opacity 设置为 0 会导致玻璃效果完全不渲染,你可以使用 Reanimated 在切换 glassEffectStyle 为所需样式和 'none' 之间时,为包装视图的 opacity 做动画。

Glass effect opacity animation workaround
import { GlassView } from 'expo-glass-effect'; import { StyleSheet, Text, View, Image, Pressable } from 'react-native'; import Animated, { useAnimatedProps, useAnimatedStyle, useSharedValue, withTiming, } from 'react-native-reanimated'; const AnimatedGlassView = Animated.createAnimatedComponent(GlassView); export default function GlassOpacityAnimationExample() { const fadeOpacity = useSharedValue(0); const glassViewProps = useAnimatedProps(() => { const glassEffectStyle = fadeOpacity.value > 0.01 ? 'regular' : 'none'; return { glassEffectStyle, style: { width: 150, height: 100, borderRadius: 12, position: 'absolute', }, }; }); const fadeOpacityStyle = useAnimatedStyle(() => ({ position: 'absolute', opacity: fadeOpacity.value, width: 150, height: 100, borderRadius: 12, })); return ( <> <Text style={styles.title}>不透明度动画变通方案(iOS 26.1+)</Text> <View style={styles.backgroundContainer}> <Image style={styles.backgroundImage} source={{ uri: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400&h=400&fit=crop', }} /> <Animated.View style={fadeOpacityStyle}> <AnimatedGlassView animatedProps={glassViewProps} /> </Animated.View> </View> <Pressable style={styles.toggleButton} onPress={() => { fadeOpacity.value = withTiming(fadeOpacity.value > 0.5 ? 0 : 1, { duration: 500 }); }}> <Text style={styles.toggleButtonText}>切换玻璃可见性</Text> </Pressable> </> ); } const styles = StyleSheet.create({ title: { fontSize: 20, fontWeight: 'bold', }, backgroundContainer: { height: 300, borderRadius: 12, overflow: 'hidden', position: 'relative', }, backgroundImage: { width: '100%', height: '100%', }, toggleButton: { backgroundColor: '#007AFF', paddingVertical: 14, paddingHorizontal: 24, borderRadius: 12, alignItems: 'center', }, toggleButtonText: { color: '#fff', fontSize: 16, fontWeight: '600', }, });

isLiquidGlassAvailable

isLiquidGlassAvailable 函数可让你检查编译后的应用中是否可用 Liquid Glass 效果。 它会验证系统和编译器版本,以及 Info.plist 设置。

import { isLiquidGlassAvailable } from 'expo-glass-effect'; export default function CheckLiquidGlass() { return ( <Text> {isLiquidGlassAvailable() ? 'Liquid Glass effect is available' : 'Liquid Glass effect is not available'} </Text> ); }

isGlassEffectAPIAvailable

isGlassEffectAPIAvailable 函数会检查设备运行时是否可用 Liquid Glass API。

警告 添加此 API 是因为某些 iOS 26 beta 版本没有提供 Liquid Glass API,这可能会导致崩溃。在应用中使用 GlassView 之前,你应该先检查这一点以确保兼容性。更多信息请参阅 GitHub issue #40911

import { isGlassEffectAPIAvailable } from 'expo-glass-effect'; export default function CheckGlassEffectAPI() { return ( <Text> {isGlassEffectAPIAvailable() ? 'Glass Effect API is available' : 'Glass Effect API is not available'} </Text> ); }

API

import { GlassView, GlassContainer, isLiquidGlassAvailable, isGlassEffectAPIAvailable, } from 'expo-glass-effect';

Components

GlassContainer

iOS
tvOS

Type: React.Element<GlassContainerProps>

GlassContainerProps

ref

iOS
tvOS
Optional • Type: Ref<View>

spacing

iOS
tvOS
Optional • Type: number • Default: undefined

The distance at which glass elements start affecting each other. Controls when glass elements begin to merge together.

Inherited Props

GlassView

iOS
tvOS

Type: React.Element<GlassViewProps>

GlassViewProps

colorScheme

iOS
tvOS
Optional • Type: GlassColorScheme • Default: 'auto'

The color scheme for the glass effect appearance. Use this to override the system appearance when your app has its own theme toggle.

glassEffectStyle

iOS
tvOS
Optional • Literal type: union • Default: 'regular'

Glass effect style to apply to the view. Can be a simple string ('clear', 'regular', 'none') or a config object for controlling animation behavior.

Acceptable values are: GlassStyle | GlassEffectStyleConfig

isInteractive

iOS
tvOS
Optional • Type: boolean • Default: false

Whether the glass effect should be interactive.

ref

iOS
tvOS
Optional • Type: Ref<View>

tintColor

iOS
tvOS
Optional • Type: string

Tint color to apply to the glass effect.

Inherited Props

Methods

isGlassEffectAPIAvailable()

iOS

Checks whether the Liquid Glass API is available at runtime on the device.

This method was added because some iOS 26 beta versions do not have this API available, which can lead to crashes. You should check this before using GlassView and GlassContainer in your app to ensure compatibility.

Returns:
boolean

isLiquidGlassAvailable()

iOS
tvOS

Indicates whether the app is using the Liquid Glass design. The value will be true when the Liquid Glass components are available in the app.

This only checks for component availability. The value may also be true if the user has enabled accessibility settings that limit the Liquid Glass effect. To check if the user has disabled the Liquid Glass effect via accessibility settings, use AccessibilityInfo.isReduceTransparencyEnabled().

Returns:
boolean

Types

GlassColorScheme

iOS
tvOS

Literal Type: string

Acceptable values are: 'auto' | 'light' | 'dark'

GlassEffectStyleConfig

iOS
tvOS
PropertyTypeDescription
animate(optional)boolean

Whether to animate the style change.

Default:false
animationDuration(optional)number

Duration of the animation in seconds. Uses system default if not specified.

styleGlassStyle

The glass effect style to apply.

GlassStyle

iOS
tvOS

Literal Type: string

Acceptable values are: 'clear' | 'regular' | 'none'