๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿš€ Development/React Native

[React Native] ๋ ˆ์ด์•„์›ƒ ์Šคํƒ€์ผ๋ง

by Jay Din 2023. 6. 15.
728x90
๋ฐ˜์‘ํ˜•
๋ฐ˜์‘ํ˜•

View

ํฌ๊ธฐ(๋ฒ”์œ„)๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์†์„ฑ. flex์™€ width, height์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค.

 

flex

ํฌ๊ธฐ๋ฅผ ๋น„์œจ๋กœ ์„ค์ •

 

export default class App extends Component<Props> {
 render() {
   return (
    <View style={styles.container}>
      <View style={styles.case1} />
      <View style={styles.case2} />
      <View style={styles.case3} />
    </View>
   );
 }
}
const styles = StyleSheet.create({
 container: {
   flex: 1,
   backgroundColor: 'white',
 },
 case1: {
   flex: 1,
   backgroundColor: 'red',
 },
 case2: {
   flex: 3,
   backgroundColor: 'green',
 },
 case3: {
   flex: 1,
   backgroundColor: 'blue',
 }, });

์‹คํ–‰ํ™”๋ฉด

width, height

์ˆซ์ž๋ฅผ ๋„ฃ์œผ๋ฉด ๊ณ ์ • ํฌ๊ธฐ, %๋ฅผ ๋„ฃ์œผ๋ฉด ํ™”๋ฉด ํฌ๊ธฐ์— ๋”ฐ๋ฅธ ์ƒ๋Œ€์  ํฌ๊ธฐ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

import React, {Component} from 'react';
import {StyleSheet, Text, View} from 'react-native';
type Props = {};
export default class App extends Component<Props> {
 render() {
   return (
    <View style={styles.container}>
      <View style={styles.case1} />
      <View style={styles.case2} />
      <View style={styles.case3} />
      <View style={styles.case4} />
      <View style={styles.case5} />
    </View>
   );}}
const styles = StyleSheet.create({
 container: {
   flex: 1,
   flexDirection: ‘row’, // ํ˜น์€ column
 },
 case1: {
   width:100,
   height:100,
   backgroundColor: 'red',
 },
 case2: {
   width:50,
   height:100,
   backgroundColor: 'green',
 },
 case3: {
   width:150,
   height:70,
   backgroundColor: 'blue',
 },
 case4: {
   width:"100%",
   height:70,
   backgroundColor: 'black',
 },
 case5: {
   width:"50%",
   height:"50%",
   backgroundColor: 'yellow',
 },
});

์‹คํ–‰ ํ™”๋ฉด

 

๋ฐ˜์‘ํ˜•

๋ฐฐ์น˜(Flex Direction)์™€ ์ •๋ ฌ(justify content, align items)

View์—์„œ ์ž์‹ ์š”์†Œ๋“ค์„ ๊ฐ€๋กœ, ์„ธ๋กœ๋กœ ์Œ“๊ธฐ ์œ„ํ•ด์„œ Flex Direction ์Šคํƒ€์ผ์„ ์‚ฌ์šฉํ•œ๋‹ค.

Flex Direction์€ row(ํ–‰), column(์—ด) ์†์„ฑ์ด ์žˆ์œผ๋ฉฐ row๋Š” ๊ฐ€๋กœ๋ฐฐ์น˜, column์€ ์„ธ๋กœ๋ฐฐ์น˜์ด๋‹ค.

์œ„ ์ฝ”๋“œ ์ฃผ์„ ์ฐธ๊ณ 

 

Align Items

๋ฐฐ์น˜๋ฐฉํ–ฅ(Flex Direction)์œผ๋กœ๋ถ€ํ„ฐ ์ˆ˜์งํ•œ ์ •๋ ฌ์„ ๋œปํ•œ๋‹ค.

flex-start, center, flex-end, stretch, vaseline์˜ 5๊ฐ€์ง€ ์†์„ฑ์ด ์žˆ๋‹ค.

flex๋Š” ๊ฐ€๋กœ ์ •๋ ฌ์„ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์ธก(์‹œ์ž‘์ ), center๋Š” ๊ฐ€์šด๋ฐ, flex-end๋Š” ์šฐ์ธก(๋์ )์„ ๋œปํ•œ๋‹ค.

stretch๋Š” ์ •๋ ฌ ๋ฐฉํ–ฅ์˜ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜์„ ๋•Œ, flex-start ์ง€์ ๋ถ€ํ„ฐ flex-end์ง€์ ๊นŒ์ง€ ์ญ‰ ๋Š˜๋ฆฌ๋Š” ์†์„ฑ์ด๋‹ค.

 

TouchableOpacity

TouchableOpacity ์ปดํฌ๋„ŒํŠธ๋Š” ‘ํ„ฐ์น˜ ์ด๋ฒคํŠธ(onPress ๋“ฑ)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” View’๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

TouchableOpacity ์ปดํฌ๋„ŒํŠธ์— Text ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋„ฃ์–ด์„œ ๋ฒ„ํŠผ์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ๊ฒƒ์ด๋‹ค.

Opacity๋Š” ํˆฌ๋ช…๋„๋ฅผ ๋œปํ•˜๋Š”๋ฐ, ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํˆฌ๋ช…๋„๊ฐ€ ๋“ค์–ด๊ฐ€์„œ ์ด ๋ฒ„ํŠผ์— ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚˜๊ณ  ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ

์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฏธ์ง€๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ source๋ผ๋Š” ์†์„ฑ์„ ๋ช…์‹œํ•ด์ค˜์•ผํ•œ๋‹ค.

source={{uri: ‘์ด๋ฏธ์ง€์ฃผ์†Œ’}}๋กœ ์™ธ๋ถ€ ์ฃผ์†Œ๋ฅผ ํ†ตํ•ด ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜,

source={require(‘๋กœ์ปฌ๊ฒฝ๋กœ’)}๋ฅผ ํ†ตํ•ด ๋‚ด๋ถ€์˜ ์ด๋ฏธ์ง€๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์™ธ๋ถ€ ์ด๋ฏธ์ง€๋Š” ๋Œ€์ƒ ์„œ๋ฒ„์˜ ์ƒํƒœ์— ๋”ฐ๋ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค.

 

resizeMode

์ปดํฌ๋„ŒํŠธ ํฌ๊ธฐ์— ๋งž๊ฒŒ ์ด๋ฏธ์ง€๋ฅผ ์กฐ์ ˆํ•˜๋Š” ์Šคํƒ€์ผ์„ resizeMode๋ผ๊ณ  ํ•˜๋ฉฐ, ๋Œ€ํ‘œ์ ์œผ๋กœ cover๊ณผ contain ์†์„ฑ์ด ์žˆ๋‹ค.

 

cover

style={{height:'100%',width:'100%',resizeMode:'cover'}}

cover๋Š” Image ์ปดํฌ๋„ŒํŠธ์˜ ๊ธฐ๋ณธ ์†์„ฑ์ด๋‹ค.

์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ ์„ธ๋กœ ์ค‘ ์ข์€ ๋ถ€๋ถ„์ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์˜ 100%๋ฅผ ์ฐจ์ง€ํ•  ๋•Œ๊นŒ์ง€ ์ด๋ฏธ์ง€๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค.

 

contain

style={{height:'100%',width:'100%',resizeMode:'contain'}}

cover์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๊ฐ€๋กœ ์„ธ๋กœ ์ค‘ ๋„“์€ ๋ถ€๋ถ„์ด 100%๋ฅผ ์ฐจ์ง€ํ•  ๋•Œ๊นŒ์ง€๋งŒ ์ด๋ฏธ์ง€๋ฅผ ๋Š˜๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค.

(์ขŒ) cover / (์šฐ) contain

 

728x90
๋ฐ˜์‘ํ˜•