Maen
Maen

Reputation: 10698

Rotated TouchableOpacity crashes on Android when selected

I rotate a TouchableOpacity (without any animation) as this:

transform: [
  { rotate: '45deg' }
]

It works fine on iOS, but crashes on Android (see tested versions below):

java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double

I can't put a Double on rotate, as I get a Invariant violation. What can I do?


Environment :

Packages:(wanted => installed)

Tested on:

Direct reference to this issue on Github

Upvotes: 0

Views: 697

Answers (2)

Harsha Koshila
Harsha Koshila

Reputation: 161

Magic Answer You can rotate View instead TouchableOpacity. Change same width and height separately

<TouchableOpacity  style={{  height: 100, width: 20 }} >
                <View style={{  height: 20, width: 100, transform: [{ rotate: '90deg' }] }}>
                       <Text>PRESS</Text>
                </View>
</TouchableOpacity>

Upvotes: 0

Maen
Maen

Reputation: 10698

Conditionally use radians with Platform, they'll get converted to double without error:

import {Platform} from 'react-native'
…
transform: [
  {rotate: (Platform.OS === 'ios') ? '45deg' : (3.14159/4)+'rad'}
]

This will then display fine.

But, that rotated TouchableOpacity loses its touchable behavior by the way.

To fix this, use a child view to apply your rotation on :

<TouchableOpacity onPress={…}>
  <View style={styles.yourRotation}>
     …
  </View>
</TouchableOpacity>

Upvotes: 1

Related Questions