我使用了REACT-Native、EXPO和REACT-Native-SVG在我的Web应用程序中呈现了一个SVG.不幸的是,在改变Web应用程序窗口大小的过程中,我很难让SVG保持相同的外观.

我们的目标是创造一个"波浪式"的头球.我已经包括了一个snack here that reproduces my exact issue以及下面的一些代码.如果您运行零食,请点击Web模拟器中的弹出箭头来更改窗口大小以查看问题.

How can I render an svg to look the same dynamically across different window sizes?

EDIT 1::我意识到我正在寻找的答案要求高度是静态的.我希望宽度始终是窗口的宽度,而高度是恒定的,无论屏幕大小如何都不会改变.

感谢您的真知灼见!我比你想象的更感激.

SvgComponent.js

const SvgComponent = (props) => (
  <View  style={[styles.header,
    {
      transform: [{rotate: '180deg'}],
    },
  ]}>
  <Svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320" {...props}>
    <Path
      fill="#09f"
      d="m0 128 80-10.7C160 107 320 85 480 96c160 11 320 53 480 64s320-11 400-21.3l80-10.7v192H0Z"
    />
  </Svg>
  </View>
)

App.js

export default function App() {
  return (
    <View style={styles.container}>
        <Home/>
        <TopHeader/>
    </View>
  );
}

家,JS,家.

export default function Home() {
  return (
    <View style={styles.container}>
        <Text style={styles.title}>Home</Text>
    </View>
  );
}
const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    height: height,
    width: width
  },
  title: {
    fontSize: 175, 
    color: 'white'
  }
});

TopHeader.js

export default function TopHeader(props) {
  const {menuActivated, switchTabs, setMenuActivated, headerTriggerd, setHeaderTriggered} = props;
  const [FS, setFS] = useState({ "width": Math.min(window.innerWidth), "height": Math.min(window.innerHeight)})
    useEffect(() => {
      window.addEventListener('resize', changeTitleSize);
      return () => {
          window.removeEventListener('resize', changeTitleSize);
      }
    }, []);
    function changeTitleSize(){
      setFS({ "width": Math.min(window.innerWidth), "height": Math.min(window.innerHeight)})
    }
  return (
    <View style={styles.container}>
      <SvgComponent width={FS.width} height={FS.width / 4} />
      <WebsiteHeader />
    </View>
  );
}

推荐答案

你调整SVG大小的逻辑是正确的,但你的布局有问题,我已经在这个snack中更新了它.

我已将TabHeader移到Home上方,并从TabHeader中删除了绝对样式.

React-native相关问答推荐

React onPress 立即执行(渲染时间),但是一旦用箭头函数替换它就不起作用

React Native 响应式图像宽度,而图像的宽度/高度比保持不变

ReactJS 应用多种内联样式

无法在 React Native iOS 模拟器中按 URI 显示图像

React Native 入门元素Bundle 失败,出现 Unexpected Token 错误

如何更改 React Native(android)应用程序的远程推送通知图标

为了回调 URL 的目的,在 Expo 中运行的 React Native 元素的 info.plist 在哪里?

异步(async)/等待(await)和递归

React-native 在浏览器中打开链接并返回应用程序

如何判断 React Native 中的 AsyncStorage 中是否存在密钥? getItem() 总是返回一个promise对象

将图标添加到drawer抽屉react-navigation v5

在Android上更改高度时react-native TextInput显示错误

React Native:当我从 XCode 运行应用程序时看不到 console.logs

如何将捕获的图像与 react-native-camera 一起使用

如何将props传递给 React Navigation 导航器中的组件?

react-native:多色文本视图

以编程方式读取 app.json(或 exp.json)

React Native - 为什么我需要 babel 或 webpack?

React Native:文本 node 的 Auto宽度

如何定义react-native应用程序的入口点