question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

appear button in ios

See original GitHub issue

All is working fine in Android but in ios have an extra button showing current date and I need to press on that instead of pressing Show Picker.
In my code I am using TouchableOpacity instead of Button. Any advise?

Below are my code. `export const AppDateTimePicker = () => { const [date, setDate] = useState(new Date()); const [mode, setMode] = useState(‘date’); const [show, setShow] = useState(false); const [chosenDate, setChosenDate] = useState(‘dd/mm/yyyy’);

const onChange = (event, selectedDate) => { const currentDate = selectedDate || date; setShow(Platform.OS === ‘ios’); setDate(currentDate);

let tempDate = new Date(currentDate);
let showDate = [tempDate.getDate(), (tempDate.getMonth() + 1), tempDate.getFullYear()].join('/');
setChosenDate(showDate);

};

const showMode = (currentMode) => { setShow(true); setMode(currentMode); };

const showDatepicker = () => { showMode(‘date’); };

return ( <View style={styles.container}> <View style={styles.dateContent}> <TouchableOpacity onPress={(showDatepicker)} > <Text style={styles.textStyle}>{chosenDate}</Text> <MaterialCommunityIcons name="calendar" size={24} style={styles.icon}/> </TouchableOpacity> {/* <Button onPress={showDatepicker} title="Show date picker!" /> */} </View>

  {show && (
    <DateTimePicker
      testID="dateTimePicker"
      value={date}
      mode={mode}
      display="default"
      onChange={onChange}
    />
  )}
</View>

); };

const styles = StyleSheet.create({ container: { borderBottomWidth: 1, borderBottomColor: colors.grey, width: ‘100%’, paddingTop: 8, paddingBottom: 8, marginBottom: 30, marginTop: -10 }, icon: { position: ‘absolute’, right: 0, // top: 12 }, dateContent: { width: ‘100%’, padding: 8, color: colors.primary }, textStyle: { color: colors.primary } }); `

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

2reactions
SymntxHomendra51commented, Jul 20, 2022

Save your time and use this package instead https://github.com/mmazzarolo/react-native-modal-datetime-picker

2reactions
M-Monncommented, Mar 11, 2022

Solved. Need to wrapped inside Modal and display as ‘spinner’ for ios. You can check below code. I found the solution from the link below: https://www.youtube.com/watch?v=lpIEpggB6o4 Thanks to A3 Programming.

import React, {useState} from 'react';
import {View, Platform, Text, StyleSheet, Modal, TouchableOpacity} from 'react-native';
import DateTimePicker from '@react-native-community/datetimepicker';
import { MaterialCommunityIcons } from '@expo/vector-icons';
import colors from '../config/colors';

function AppDateTimePicker (props) {
  const [date, setDate] = useState(new Date());
  const [mode, setMode] = useState('date');
  const [show, setShow] = useState(false);
  const [chosenDate, setChosenDate] = useState('dd/mm/yyyy');

  const onChange = (event, selectedDate) => {
    const currentDate = selectedDate || date;
    setShow(Platform.OS === 'ios');
    setDate(currentDate);
    let tempDate = new Date(currentDate);
    let showDate = [tempDate.getDate(), (tempDate.getMonth() + 1), tempDate.getFullYear()].join('/');
    setChosenDate(showDate);
  };

  const showMode = (currentMode) => {
    setShow(true);
    setMode(currentMode);
  };

  const showDatepicker = () => {
    showMode('date');
  };

  const hideDatepicker = () => {
    setShow(false);
  };

  const onCancelPress = () => {
    setDate(date);
    setShow(false);
  }

  const onDonePress = () => {
    props.onDateChange(date);
    setShow(false);
  }

  const renderDatePicker = () => {
    return(
      <DateTimePicker
        testID="dateTimePicker"
        value={new Date(date)}
        mode={mode}
        display={Platform.OS === 'ios' ? 'spinner' : 'default'}
        onChange={onChange}/>
    );
  }

  return (
    <View style={styles.container}>
      <View style={styles.dateContent}>
        <TouchableOpacity onPress={showDatepicker} >
          <Text style={styles.textStyle}>{chosenDate}</Text>
          <MaterialCommunityIcons name="calendar" size={24} style={styles.icon}/>
        </TouchableOpacity>
      </View>
      {Platform.OS != 'ios' && show && renderDatePicker()}
      {Platform.OS === 'ios' && show && (
        <Modal
          transparent={true}
          animationType='slide'
          visible={show}
          supporDirections={['portrait']}
          onRequestClose={hideDatepicker}>
          <View style={styles.modalContentView}>
            <TouchableOpacity
              style={{ flex: 1, backgroundColor: colors.lightGrey, }}
              onPress={onCancelPress}>
              <Text style={styles.modalBtnText}>Cancel</Text>
            </TouchableOpacity>
            <TouchableOpacity
              style={{backgroundColor: colors.lightGrey}}
              onPress={onDonePress}>
              <Text style={styles.modalBtnText}>Done</Text>
            </TouchableOpacity>
          </View>
          <View style={{backgroundColor: colors.lightGrey}}>
            {renderDatePicker()}
          </View>
        </Modal>
      )}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    borderBottomWidth: 1,
    borderBottomColor: colors.grey,
    width: '100%',
    paddingTop: 8,
    paddingBottom: 0,
    marginTop: -10
  },
  icon: {
    position: 'absolute',
    right: 0,
    bottom: 0
  },
  dateContent: {
    width: '100%',
    padding: 8,
    color: colors.primary
  },
  textStyle: {
    color: colors.primary,
  },
  modalContentView:{
    flex: 1, 
      alignItems: 'flex-end',
      flexDirection: 'row', 
      justifyContent: 'center',
  },
  modalBtnText: {
    color: colors.primary,
    fontWeight: 'bold',
    padding: 20
  }
});

export default AppDateTimePicker;


Read more comments on GitHub >

github_iconTop Results From Across the Web

Use AssistiveTouch on your iPhone, iPad, or iPod touch
When you turn on AssistiveTouch, you see a button appear onscreen. You can drag the button to any edge of the screen, where...
Read more >
How to Get a Home Button on Your iPhone's Screen
1. Start the Settings app on your iPhone. · 2. Tap Accessibility on an iPhone running iOS 13, 14, and 15. · 3....
Read more >
How to Get Home Button on iPhone Screen - YouTube
iphone12pro #homebutton #assistivetouchIn this video we show a quick tip on getting a virtual home button on the newer Apple iPhone.
Read more >
How To Add Home Button on iPhone 14 Pro Screen! - YouTube
Amazon Daily Deals: http://saunders.gg/amazon❤️ If you enjoy this content, please consider a paid "super thanks" in the video or becoming ...
Read more >
how to hide/show a button in swift - Stack Overflow
To hide a button, use button.hidden = true https://developer.apple.com/library/ios ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found