Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native DateTimePicker not Opening in IOS

Datetime Picker is not opening i'm using react-native with react-hooks, in Android works fine . but its not opening in IOS and not showing any error.

i just created local component for datepicker so i can use it for both android and ios. for android its going fine IOS is the issue, its not responding

i done pod clean and install also no luck

const DatePicker =({}) =>{
   const [state, setState] = useState({
     date:  new Date(),
     mode: 'date',
     show: false
 });

 const showPicker = mode => {
     setState(prevState => ({
     ...prevState,
     show: Platform.OS === 'ios',
     mode
     }));
 };

 const datePicker = () => {
     showPicker('datetime');
 };


 return(
     <>
         <View>
             <View style={styles.createBorder}>
                 <TouchableHighlight
                     underlayColor={disable ? 'transperant' : ''}
                     onPress={!disable && timePicker}
                 >
                     <View style={styles.datePickerBox}>
                     <Text
                         style={[styles.datePickerText, { width: width - 60 }]}
                     >
                         {state.date}
                     </Text>
                     </View>
                 </TouchableHighlight>
             </View>
             {state.show && Platform.OS === 'ios' && (
                 <DateTimePicker
                 style={[styles.inputBackground, {width: '100%' }]}
                 value={state.date}
                 mode="datetime"
                 is24Hour={false}
                 display="default"
                 onChange={setDate}
                 />
             )}
         </View>
     </>
 );  
}  
export default DatePicker;
like image 843
Kathirpandian K Avatar asked Feb 03 '20 07:02

Kathirpandian K


3 Answers

I had the same problem as of June 26, 2020, with this version: "@react-native-community/datetimepicker": "2.2.2".

I just added style with width and the datetimepicker displayed in iOS:

style={{width: 320, backgroundColor: "white"}}

I also added backgroundColor so that it becomes opaque white.

Here is my code:

<DateTimePicker
    value={user.birthdate ? new Date(user.birthdate) : new Date()}
    mode='date'
    display="default"
    onChange={(event, date) => {
       //...some code here
       setBirthdateTouched(true);
       setModalVisible(!modalVisible);
    }}
    style={{width: 320, backgroundColor: "white"}} //add this

/>

Enjoy coding!

like image 160
Meo Flute Avatar answered Nov 14 '22 17:11

Meo Flute


I just move to react-native-modal-datetime-picker its working fine now

like image 44
Kathirpandian K Avatar answered Nov 14 '22 18:11

Kathirpandian K


<DatePicker
iOSDatePickerComponent={(props) => (
                              <RNDatePicker
                                {...props}
                                display={
                                  Platform.OS === "ios" ? "spinner" : "default"
                                }
                              />
                            )} ..../>

just add this in your datePicker component will work fine ;)

like image 4
ahmed mersal Avatar answered Nov 14 '22 16:11

ahmed mersal