如何使 @gorhom/bottom-sheet 居中,而不是 100% 宽度?

huangapple go评论54阅读模式
英文:

How to center @gorhom/bottom-sheet with not 100% width?

问题

I am using https://gorhom.github.io/react-native-bottom-sheet/ library and I do not want the BottomSheet to take full width of the screen. So I limit it as

<BottomSheet snapPoints={snapPoints} style={{width: '80%'}}>
      ...
</BottomSheet>

and try to center it as justifyContent: 'center' in View wrapping the BottomSheet. However, it does not work as expected.

如何使 @gorhom/bottom-sheet 居中,而不是 100% 宽度?

So the question is: how do I reduce the width of the BottomSheet and center it?

英文:

I am using https://gorhom.github.io/react-native-bottom-sheet/ library and I do not want the BottomSheet to take full width of the screen. So I limit it as

&lt;BottomSheet snapPoints={snapPoints} style={{width: &#39;80%&#39;}}&gt;
      ...
&lt;/BottomSheet&gt;

and try to center it as justifyContent: &#39;center&#39; in View wrapping the BottomSheet. However, it does not work as expected.

如何使 @gorhom/bottom-sheet 居中,而不是 100% 宽度?

So the question is: how do I reduce the width of the BottomSheet and center it?

答案1

得分: 1

有一个快速的解决方法:

import { Dimensions, ... } from "react-native";

...

<BottomSheet
  snapPoints={snapPoints}
  style={{ marginHorizontal: Dimensions.get("screen").width * 0.1 }}
>
  ...
</BottomSheet>

它的作用是:计算两侧(右侧和左侧)的边距:每侧应该有10%的边距(总宽度为80%)。

希望对您有所帮助! 如何使 @gorhom/bottom-sheet 居中,而不是 100% 宽度?

英文:

There is a quick workaround:

import {Dimensions, ...} from &quot;react-native&quot;;

...

&lt;BottomSheet
  snapPoints={snapPoints}
  style={{marginHorizontal: Dimensions.get(&quot;screen&quot;).width*0.1}}
&gt;
  ...
&lt;/BottomSheet&gt;

What it does: It calculates the margin to both sides (right and left): There should be 10% margin to each side (in total a width of 80%).

Hope that helps! 如何使 @gorhom/bottom-sheet 居中,而不是 100% 宽度?

huangapple
  • 本文由 发表于 2023年7月6日 22:34:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定