5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

react-native-safe-area-viewを使ってiOSのSafeAreaをよしなにする

Posted at

React Nativeでネイティブアプリを作っていると、Appbar(Toolbar)をセットしたくなりますよね。

ただ、単純に「セットしただけ」だと、SafeAreaと思いっきりかぶってしまいます。
こんな感じ。

App.tsx
export default class App extends React.Component<Props> {
  render() {
    return (
        <Appbar>
          <Appbar.Content title="MyProfile" />
          <Appbar.Action icon="mail" onPress={this.showAlert} />
          <Appbar.Action icon="call" onPress={this.showAlert} />
        </Appbar>
        <View style={styles.container}>
          <View style={styles.body}>
            <Text style={styles.welcome}>Welcome to React Native!</Text>
            <Text style={styles.instructions}>To get started, edit App.js</Text>
            <Text style={styles.instructions}>{instructions}</Text>
            <Button icon="add-a-photo" mode="contained" onPress={this.showAlert}>Press me</Button>
          </View>
        </View>
    );
  } 
  showAlert() {
    Alert.alert("こんにちは世界!");
  }

ここにreact-native-safe-area-viewのSafeAreaViewを使うと、SafeAreaをよしなに避けてくれます。
こうなります。

スクリーンショット 2019-04-22 15.41.06.png
App.tsx
export default class App extends React.Component<Props> {
  render() {
    return (
      <SafeAreaView style={styles.container}>
        <Appbar>
          <Appbar.Content title="MyProfile" />
          <Appbar.Action icon="mail" onPress={this.showAlert} />
          <Appbar.Action icon="call" onPress={this.showAlert} />
        </Appbar>
        <View style={styles.container}>
          <View style={styles.body}>
            <Text style={styles.welcome}>Welcome to React Native!</Text>
            <Text style={styles.instructions}>To get started, edit App.js</Text>
            <Text style={styles.instructions}>{instructions}</Text>
            <Button icon="add-a-photo" mode="contained" onPress={this.showAlert}>Press me</Button>
          </View>
        </View>
      </SafeAreaView>
    );
  } 
  showAlert() {
    Alert.alert("こんにちは世界!");
  }
}

React Native楽しい!!!!!!!!

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?