Help us understand the problem. What is going on with this article?

React Nativeでオーバーレイなフッターを実装する

More than 1 year has passed since last update.

こういうの作りたいよねというお話です。完全にスタイルのお話。

ss.png

サンプルはGitHubに上げときました。Expo製です。

https://github.com/Nkzn/RNFooterOverlaySample

簡単では?

できちゃえば簡単なんですが、「とりあえず縦とか横に並べたらええんやろ!!!」というFlexbox脳のまま立ち向かうと死ぬので、ひとまず一例として置いとかせてください。

3行で

説明面倒なのでコード読んでください。

コード

App.js
// @flow
import React from 'react';
import { StyleSheet, View, Text, Button, ScrollView } from 'react-native';

type State = {
  footerWidth: number;
}

export default class App extends React.Component<any, State> {

  state: State = {
    footerWidth: 0
  };

  onContentLayout(layout: { width: number }) {
    this.setState({ footerWidth: layout.width });
  }

  render() {

    const footerStyle = {
      ...StyleSheet.flatten(styles.footerOverlay),
      width: this.state.footerWidth
    };

    return (
      <View style={styles.container}>
        <View style={styles.header} />
        <View
          style={styles.content}
          onLayout={ event => this.onContentLayout(event.nativeEvent.layout) }>
          <ScrollView style={styles.scroll} contentContainerStyle={styles.scrollContentContainer}>
            <View>
              { [1,2,3,4,5,6,7,8,9,10,11,12].map(n => <Text key={`${n}`} style={{ fontSize: 60 }}>{n}</Text>) }
            </View>
          </ScrollView>
          <View style={footerStyle}>
            <Button title="Close" color="#FFF" onPress={() => {}} />
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#AAA',
  },
  header: {
    height: 96,
    backgroundColor: "#00FF00",
  },
  content: {
    flex: 1,
  },
  scroll: {
    flex: 1,
    backgroundColor: "#FF0000",
    opacity: 0.5
  },
  scrollContentContainer: {
    paddingHorizontal: 16,
    paddingBottom: 104, // フッター分を底上げ
  },
  footerOverlay: {
    flex: 1,
    height: 96,
    position: "absolute",
    bottom: 0,
    backgroundColor: "#0000FF",
    opacity: 0.5,
    justifyContent: "center",
  }
});

まとめ

もうちょっとスマートな方法がありそうな気もするので誰か教えてください。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした