React Native DateTimePicker not Opening in IOS
Asked Answered
U

4

13

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;
Unrestraint answered 3/2, 2020 at 7:16 Comment(2)
Do you have any expo project?Oodles
sorry don't have expo projectUnrestraint
U
6

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

Unrestraint answered 18/3, 2020 at 8:59 Comment(0)
S
16

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!

Stutman answered 26/6, 2020 at 16:2 Comment(1)
This issue is still there with version: "@react-native-community/datetimepicker": "3.0.9". This workaround helped!Flogging
U
6

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

Unrestraint answered 18/3, 2020 at 8:59 Comment(0)
A
6

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

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

Averir answered 2/3, 2021 at 11:34 Comment(0)
D
2

instead of using display='default', use this one display='spinner' it will affect on both Android and iOS.

Disposure answered 30/12, 2022 at 20:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.