scrollView 宽高无效

在使用React Native的ScrollView时遇到宽高设置无效的问题。环境为0.46.4版本,问题出现在Android和iOS平台上。预期效果是设置固定宽高,但实际效果并未生效。解决方案是删除宽度和高度样式,或设置`style={{flex: 1}}`。ScrollView的样式会受到其父组件的影响。详细讨论见GitHub issue #15581。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Environment

  1. react-native -v:0.46.4
  2. node -v:v8.3.0
  3. npm -v:5.3.0
  4. yarn --version :0.27.5

    • Target Platform: android && ios
    • Development Operating System: mac10.12.5
    • Build tools:webstorm

Steps to Reproduce

code like that:

   renderTab = ()=> {
        return (
            <View style={{width:WIDTH,height:SCALE(100),backgroundColor:'blue',flexDirection:'row', justifyContent:'space-around', alignItems:'center'}}>
            <View><Text>我发起的</Text></View>
            {(this.state.data&&this.state.data.myIsLeader===1)?<View><Text>带我审批</Text></View>:null}
            {(this.state.data&&this.state.data.myIsLeader===1)?<View><Text>我已审批</Text></View>:null}
        </View>)
    };

    render(){
        return (
            <View style={{width:WIDTH,height:HEIGHT}}>
                <ScrollView
                    style={{backgroundColor:'red',width:WIDTH,height:HEIGHT-SCALE(200)}}>

            </ScrollView>
                {this.renderTab()}
        </View>
        );
    }

期望效果

like that:
expected

实际效果

actual

解决:
删除 style={{width:WIDTH,height:HEIGHT}}
或者设置style={{flex:1}} 。
scrollview的属性不光和自己有关还和父组件栈的属性有关。
GitHub:https://github.com/facebook/react-native/issues/15581

### 实现 ScrollView 度根据内容自动调整 为了使 `ScrollView` 的度能够根据内容动态调整,不同平台有不同的解决方案。 #### Android 平台 在 Android 中可以通过自定义 `ScrollView` 来控制其最大度,并确保它能根据内部内容扩展: ```java public class CustomScrollView extends ScrollView { private int maxHeight; public CustomScrollView(Context context) { super(context); } public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public void setMaxHeight(int height){ this.maxHeight = height; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int newHeightSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, newHeightSpec); } } ``` 此代码片段展示了如何创建一个继承于 `ScrollView` 的类来限制它的最大度[^1]。通过覆写 `onMeasure()` 方法可以灵活地设定组件的最大尺寸而不影响其他属性。 对于 XML 布局文件中的配置,则保持默认即可,因为这里主要是针对 Java/Kotlin 层面的操作。 #### iOS 平台 iOS 上使用 Auto Layout 和 UIScrollView 结合起来可以让滚动视图的内容大小随实际需求变化而改变。具体做法如下所示: - 设置父容器(通常是 UIViewController 的 view)采用 FlexBox 或者传统的 StackView 方式管理子元素; - 给定明确的约束条件给到 scrollView 及其 contentSize; 以下是 Swift 语言下的简单例子说明如何操作: ```swift import UIKit class ViewController: UIViewController { let scrollView = UIScrollView() var contentView = UIView() override func viewDidLoad() { super.viewDidLoad() setupViews() configureConstraints() } private func setupViews(){ self.view.addSubview(scrollView) scrollView.addSubview(contentView) // Add subviews to contentView as needed... } private func configureConstraints(){ NSLayoutConstraint.activate([ scrollView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor), scrollView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor), scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor), contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor), contentView.topAnchor.constraint(equalTo: scrollView.topAnchor), contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor), contentView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor) ]) } } ``` 这段代码设置了基本的 Scroll View 构造逻辑以及必要的 Autolayout Constraints[^5]。注意这里的重点在于让 Content View 度等于 Scroll View 同时上下左右都贴紧后者,这样就能保证当有更多内容加入时会触发滚动效果而不是超出边界显示不全的情况发生。 另外一种方式是在 CSS/HTML 环境下处理类似问题,比如 WeChat 小程序开发环境里,可通过设置 `.box`,`.box-scroll` 类样式达到相同目的: ```css page{ height:100%; } .box { display: flex; flex-direction: column; height: 100vh; /*度必须指定 或者写成100%*/ overflow: hidden; } .box-scroll { flex-grow: 1; overflow-y: auto; /*必须写这一条*/ min-height: 0px; } ``` 上述 CSS 片段适用于小程序或其他基于 Web 技术栈的应用场景中实现 scroll-view 自适应页面剩余空间的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值