Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

javascript - Fit content to height of View in React Native (iOS)

I'm trying to fit dynamic content into a View of fixed height, in this case the height of the screen, without leaking over (so that vertical scrolling isn't required).

This is the opposite of other questions like this, where they want the height of the View determined by the content.

What I have so far:

  <SafeAreaView style={styles.container}>
    <ScrollView scrollEnabled={false}>
        <View style={styles.displayContainer}>
            {splitWords.map(word => (
              <Text
              style={styles.textDisplay}
              adjustsFontSizeToFit={true}
              numberOfLines={1}
              style={{fontSize: 200}}
              >
                {word}
              </Text>
            ))}
        </View>
      )}
    </ScrollView>
  </SafeAreaView>

I've tried various combinations of flex, flexShrink, flexWrap, etc and cant seem to get it. Here's the stylesheet:

const styles = StyleSheet.create({
container: {
  flex: 1,
},
textDisplay: {
  // Not sure what to try here next
},
displayContainer: {
  padding: 2,
  height: height - 130, // Height is obtained by Dimensions.get('window').height;
  width: width, // Width is obtained by Dimensions.get('window').weight;
  //alignSelf: 'stretch'
  //flex: 0,
  //flexWrap: 'nowrap',
  //flexDirection: 'column',
  //flexShrink: 1,
 },
});

I'd appreciate any help.

question from:https://stackoverflow.com/questions/66056153/fit-content-to-height-of-view-in-react-native-ios

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have a try by adding the below props in your ScrollView Component.

<ScrollView flex={{ flex: 1 }} contentContainerStyle={{ minHeight: "100%" }}>
   ...
</ScrollView>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...