如何在屏幕加载后立即进入 ScrollView 的底部?
How to go bottom of the ScrollView as soon as screen is loaded?
我有如下的 ScrollView,
render() {
switch(mode) {
case 'backNavigation':
return(
<View style={[containerView, backNavigationHeaderView]} >
<TouchableOpacity onPress={() => leftAction()} style={{marginRight: 24}}>
<Icons name='backArrow'/>
</TouchableOpacity>
<View style={{width: '81%'}}>
<ScrollView
contentContainerStyle={{alignItems: 'center'}}
horizontal
showsHorizontalScrollIndicator={false}
alwaysBounceHorizontal={false}
ref={this.setScrollViewRef}
onContentSizeChange={() => {
if(this.scrollView)
this.scrollView.scrollToEnd({ animated: true, index: -1 }, 200);
}}
>
<Text numberOfLines={1} style={[singleTitleStyle]}>{this.renderHeaderTitle(title)}</Text>
</ScrollView>
</View>
</View>
);
}
加载屏幕后出现以下错误,
Cannot read the property ScrollToEnd of undefined
我试过了。但这对我没有帮助。
有人可以帮我解决这个问题吗?谢谢。
编辑 1
这就是我设置引用的方式
setScrollViewRef(element) {
this.scrollView = element;
}
使用React.createRef()
constructor(props) {
super(props)
this.scrollView = React.createRef();
}
render() {
<ScrollView
contentContainerStyle={{alignItems: 'center'}}
horizontal
showsHorizontalScrollIndicator={false}
alwaysBounceHorizontal={false}
ref={this.scrollView}
onContentSizeChange={() => {
if(this.scrollView)
this.scrollView.current.scrollToEnd({ animated: true, index: -1 }, 200);
}}>
</ScrollView>
}
我有如下的 ScrollView,
render() {
switch(mode) {
case 'backNavigation':
return(
<View style={[containerView, backNavigationHeaderView]} >
<TouchableOpacity onPress={() => leftAction()} style={{marginRight: 24}}>
<Icons name='backArrow'/>
</TouchableOpacity>
<View style={{width: '81%'}}>
<ScrollView
contentContainerStyle={{alignItems: 'center'}}
horizontal
showsHorizontalScrollIndicator={false}
alwaysBounceHorizontal={false}
ref={this.setScrollViewRef}
onContentSizeChange={() => {
if(this.scrollView)
this.scrollView.scrollToEnd({ animated: true, index: -1 }, 200);
}}
>
<Text numberOfLines={1} style={[singleTitleStyle]}>{this.renderHeaderTitle(title)}</Text>
</ScrollView>
</View>
</View>
);
}
加载屏幕后出现以下错误,
Cannot read the property ScrollToEnd of undefined
我试过了
有人可以帮我解决这个问题吗?谢谢。
编辑 1 这就是我设置引用的方式
setScrollViewRef(element) {
this.scrollView = element;
}
使用React.createRef()
constructor(props) {
super(props)
this.scrollView = React.createRef();
}
render() {
<ScrollView
contentContainerStyle={{alignItems: 'center'}}
horizontal
showsHorizontalScrollIndicator={false}
alwaysBounceHorizontal={false}
ref={this.scrollView}
onContentSizeChange={() => {
if(this.scrollView)
this.scrollView.current.scrollToEnd({ animated: true, index: -1 }, 200);
}}>
</ScrollView>
}