12

I have a UIDatePicker in a TableView with Static Cells. The DatePicker works perfectly in iPhone version of App, but the iPad seems to compress the DatePicker so that the center column that holds the day of the month (dd) does not display. Notice that the "er" in "November" is truncated.

I verified this by changing the date style to Spanish (dd MMMM YYY) and the date displays as does the first half of the month and all of the year. The last half of each month's name is truncated.

Has anyone encountered a problem like this in porting over to iOS 8?

Never sure how much code to post. Here is most everything in the relevant class

@property (strong, nonatomic) IBOutlet UITableViewCell *dateDisplay;
@property (strong, nonatomic) IBOutlet UITableViewCell *datePickerCell;
@property (strong, nonatomic) IBOutlet UIDatePicker *datePicker;

@property (strong, nonatomic) IBOutlet UITableViewCell *unattached;
@property (strong, nonatomic) IBOutlet UITableViewCell *middleschool;
@property (strong, nonatomic) IBOutlet UITableViewCell *highschool;
@property (strong, nonatomic) IBOutlet UITableViewCell *collegiate;
@property (strong, nonatomic) IBOutlet UITableViewCell *youthclub;

@end

@implementation MeetFinderTableViewController

@synthesize divisions = _divisions;
@synthesize datePickerIndexPath = _datePickerIndexPath;
@synthesize meetDate = _meetDate;

@synthesize doSaveUserDefaults = _doSaveUserDefaults;
@synthesize divisionSelected = _divisionSelected;
@synthesize arrayOfReuseIds = _arrayOfReuseIds;
@synthesize myTV = _myTV;

# pragma SplitViewController Variables

@synthesize meetIDForSegue = _meetIDForSegue;
@synthesize meetNameForSegue = _meetNameForSegue;


#pragma SetUp Configuration

-(void) setArrayOfReuseIds:(NSArray *)arrayOfReuseIds
{
    _arrayOfReuseIds = arrayOfReuseIds;
}

-(void) setMyTV:(UITableView *)myTV
{
    _myTV = myTV;
}

-(void) setMeetDate:(NSDate *)meetDate
{
    if(_meetDate != meetDate){
        _meetDate = meetDate;
        [self setDoSaveUserDefaults:YES];
    }
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (MapViewController *)splitViewMapViewController
{
    id mvc = [self.splitViewController.viewControllers lastObject];
    if (![mvc isKindOfClass:[MapViewController class]]) {
        mvc = nil;
    }
    if (debug==1) NSLog(@"%@ = %@",NSStringFromSelector(_cmd), mvc);
    return mvc;
}


- (void)awakeFromNib
{
    [super awakeFromNib];
    self.splitViewController.delegate = self;
    if (debug==1) NSLog(@"Do I make it to %@",NSStringFromSelector(_cmd));
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.clearsSelectionOnViewWillAppear = YES;

    self.datePicker.hidden = YES;
    self.datePickerIsShowing = NO;

    [self setArrayOfReuseIds:[[NSArray alloc] initWithObjects:
      @"unattached",@"middleschool",@"highschool",@"collegiate",@"youthclub", nil]];

    [self setDivisions:[[NSArray alloc] initWithObjects:
                            @"Unattached", @"Middle School", @"High School",
                            @"Collegiate", @"Youth Club", nil]];

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"divisionPreference"]) {
        [self setDivisionSelected:[[NSUserDefaults standardUserDefaults] objectForKey:@"divisionPreference"]];
    } else [self setDivisionSelected:[NSNumber numberWithInt:highSchoolTag]];

    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"datePreferenceForMeetSearch"]) {
        [self setMeetDate:[[NSUserDefaults standardUserDefaults] objectForKey:@"datePreferenceForMeetSearch"]];
    } else [self setMeetDate:[NSDate date]];

    [self setMyTV:self.tableView];

    if (debug==1) NSLog(@"In %@ before check splitViewMapViewController",NSStringFromSelector(_cmd));

    if ([self splitViewMapViewController]) {                      // if in split view
        [self splitViewMapViewController].dateForSearch = self.meetDate;
        [self splitViewMapViewController].levelOfCompetition = [self.divisionSelected stringValue];
    }
    }

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self setupDateLabel];
    [self setUpMenuItems];

    if (debug==1) NSLog(@"Do I make it to %@",NSStringFromSelector(_cmd));

}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];

    if (self.doSaveUserDefaults) {

        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

        [userDefaults setObject:self.divisionSelected
                         forKey:@"divisionPreference"];
        [userDefaults setObject:self.meetDate
                         forKey:@"datePreferenceForMeetSearch"];

        [userDefaults synchronize];

        NSLog(@"Getting ready to set newPreferences with divisionSelected: %@ and dateOfMeet: %@", [self.divisionSelected stringValue], self.meetDate);

        MapPreferencesDataDelegate *newPreferences = [[MapPreferencesDataDelegate alloc]
                                                      initWithName:[self.divisionSelected stringValue]
                                                      dateOfMeet:self.meetDate];

        [self.delegate saveMeetSearchPreferences:newPreferences];
    }
}


- (void)setupDateLabel {

    self.dateFormatter = [[NSDateFormatter alloc] init];
    [self.dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [self.dateFormatter setTimeStyle:NSDateFormatterNoStyle];
    NSDate *defaultDate;
    if (!self.meetDate || self.meetDate == nil) {
        defaultDate = [NSDate date];
        [self setMeetDate:defaultDate];

    } else defaultDate = self.meetDate;

    self.dateDisplay.textLabel.text = [NSString stringWithFormat:@"%@",stringForDateDisplay];
    self.dateDisplay.textLabel.textColor = [self.tableView tintColor];

    self.dateDisplay.detailTextLabel.text = [self.dateFormatter stringFromDate:defaultDate];
    self.dateDisplay.detailTextLabel.textColor = [self.tableView tintColor];
}

- (void)showDatePickerCell {

    self.datePickerIsShowing = YES;
    [self.tableView beginUpdates];

    [self.tableView endUpdates];

    self.datePicker.hidden = NO;
    self.datePicker.alpha = 0.0f;

    [UIView animateWithDuration:0.25 animations:^{

        self.datePicker.alpha = 1.0f;

    }];
}

- (void)hideDatePickerCell {

    self.datePickerIsShowing = NO;

    [self.tableView beginUpdates];
    [self.tableView endUpdates];

    [UIView animateWithDuration:0.25
                     animations:^{
                         self.datePicker.alpha = 0.0f;
                     }
                     completion:^(BOOL finished){
                         self.datePicker.hidden = YES;
                     }];
}

-(void) placeCheckMarkForDivisionSelection:(NSInteger) myDivisionPreference

#pragma mark - Table view data source

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    switch (indexPath.section) {
        case 0:
        {
            if (indexPath.row == 0){
                self.datePicker.hidden = NO;
                self.datePickerIsShowing = !self.datePickerIsShowing;
                [UIView animateWithDuration:.4 animations:^{
                    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
                    [self.tableView reloadData];
                }];
            }
            break;
        }
        case 1:
        {
            UITableViewCell *cell1 = [tableView cellForRowAtIndexPath:indexPath];
            NSInteger tagForSelectedDivision = cell1.tag;

            [self placeCheckMarkForDivisionSelection:tagForSelectedDivision];
//
            [self setDivisionSelected:[NSNumber numberWithInteger:tagForSelectedDivision]];
            if ([self splitViewMapViewController]) [self splitViewMapViewController].levelOfCompetition = [self.divisionSelected stringValue];

            break;
        }
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 1) { // this is my picker cell
        if (self.datePickerIsShowing) {
            return 219;
        } else {
            return 0;
        }
    } else {
        return self.tableView.rowHeight;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return numberOfSections;
}


#pragma mark - Action methods

- (IBAction)pickerDateChanged:(UIDatePicker *)sender {

    if ([self splitViewMapViewController]) {                      // if in split view
        [self splitViewMapViewController].dateForSearch = sender.date;
        [self splitViewMapViewController].levelOfCompetition = [self.divisionSelected stringValue];

    }
    self.dateDisplay.detailTextLabel.text =  [self.dateFormatter stringFromDate:sender.date];
    self.meetDate = sender.date;
    [self setMeetDate:sender.date];
}


- (void) displayAlertBoxWithTitle:(NSString*)title message:(NSString*) myMessage cancelButton:(NSString*) cancelText
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
                                                    message:myMessage
                                                   delegate:nil
                                          cancelButtonTitle:cancelText
                                          otherButtonTitles:nil];
    [alert show];
}

#pragma SplitViewController Implementation


- (id <SplitViewBarButtonItemPresenter>)splitViewBarButtonItemPresenter
{
    id detailVC = [self.splitViewController.viewControllers lastObject];
    if (![detailVC conformsToProtocol:@protocol(SplitViewBarButtonItemPresenter)]) {
        detailVC = nil;
    }
    return detailVC;
}

- (BOOL)splitViewController:(UISplitViewController *)svc
   shouldHideViewController:(UIViewController *)vc
              inOrientation:(UIInterfaceOrientation)orientation
{
    return NO;
}

- (void)splitViewController:(UISplitViewController *)svc
     willHideViewController:(UIViewController *)aViewController
          withBarButtonItem:(UIBarButtonItem *)barButtonItem
       forPopoverController:(UIPopoverController *)pc
{
    barButtonItem.title = barButtonItemTitle;
    [self splitViewBarButtonItemPresenter].splitViewBarButtonItem = barButtonItem;
}

- (void)splitViewController:(UISplitViewController *)svc
     willShowViewController:(UIViewController *)aViewController
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
    [self splitViewBarButtonItemPresenter].splitViewBarButtonItem = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


@end

Thanks,

iPad version of datePicker

PhillipOReilly
  • 609
  • 12
  • 28
  • Just tried to implement the UI in a SplitViewController with same result. I did figure out I can change the date by flipping where the date should appear. I just cannot see it. – PhillipOReilly Oct 14 '14 at 05:11
  • Im having the exact same problem, I'm using the same storyboard for iPhone and iPad but I'm only getting it on the iPad – Jacobanks Nov 02 '14 at 16:23
  • I really need an answer on this, the version i have out on the app store is the one with this bug and people are not very happy – Jacobanks Nov 02 '14 at 16:26

7 Answers7

10

The problem seems to be associated with Autolayout. Please select the date picker in storyboard and add "Center Horizontly in container" constraint with UITableViewCell's content view. See the attached image for better understanding. https://puu.sh/cONRZ/a1f9d935b2.png

Required result after applying constraint;

https://puu.sh/cOO43/20947cf334.png

Without Constraints;

https://puu.sh/cOOnR/09f3ed8721.png

To add constraint, right click on the UIDatePicker while holding and drag the pointer to UITableViewCell's content view and release the pointer. Now select the "Center Horizontly in container" constraint. Please try and post in comments.

Community
  • 1
  • 1
user3404693
  • 555
  • 1
  • 8
  • 19
  • 2
    Okay your answer gets marked as the answer for getting me on the right track. I looked at the Constraint settings in [Apple's DateCell](https://developer.apple.com/library/IOs/samplecode/DateCell/Introduction/Intro.html) example. Finally got it to work. Thanks! @ScreamingCereal, take note! – PhillipOReilly Nov 15 '14 at 00:38
  • Center horizontally didn't work, but center vertically in container as in Apple's sample code did it for me! – Andrew Sep 11 '15 at 13:18
  • hey i have same problem but i am not using auto layout then how to solve this issue...??and i am using xib. facing issue in ios9 only – Akash Raghani Sep 22 '15 at 12:10
  • Setting the date picker's constraints for top, bottom, left, and right to the cell worked for me. (Before, it was broken when constraints were set to the content view.) – blwinters Oct 04 '15 at 03:23
1

I have been running into this same issue, the date picker months being cut off and the day of the month being invisible. None of the suggestions in this thread nor in this one: UIDatePicker Cutting Off Text & Not Displaying Days worked for me.

It started for me on iOS8. I am not using constraints. I'm not using storyboards, but I am using nib files to load the UI. My situation is on an iPad. I have a screen with a "view" mode. When a user taps a button, the view is faded out and an 'edit' mode view is faded in. The edit mode view is the same as the 'create' mode. The create mode (reached via another navigation path) doesn't cut off the date picker.

So, I just started doing crazy ideas to see if I could remove the glitch. In the end, this is my function for switching to edit mode:

//@extern
- (void) showAddEditModeUI {
    [self fadeInView:self.editMembershipView];

  //WORKAROUND - ios8 issue was cutting off the date picker in the middle
  //the solution in this stack overflow didn't work for me:
  //https://stackoverflow.com/questions/26352797/uidatepicker-not-displaying-wheel-for-day-of-the-month-dd-on-ipad-for-ios-8-1
  //but this did work
  UIDatePicker* dPicker = [[UIDatePicker alloc] initWithFrame:self.startDatePicker.frame];
  dPicker.date = self.startDatePicker.date;
  dPicker.datePickerMode = self.startDatePicker.datePickerMode;
  [self.startDatePicker removeFromSuperview];
  self.startDatePicker = dPicker;
  [self.editMembershipView addSubview:self.startDatePicker];

}

As you can see, I manually remove and re-add a UIDatePicker to the view. For whatever reason, this solved the issue. I am posting the answer here, just in case it helps someone else with this rather maddening problem.

Community
  • 1
  • 1
Mike
  • 894
  • 12
  • 19
1

try this

[myDatePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[myDatePicker setDatePickerMode:UIDatePickerModeDate];
Rahul Patel
  • 5,858
  • 6
  • 46
  • 72
user2885077
  • 702
  • 6
  • 14
1

I had this same problem on an iPhone 5 running 9.0.1. The problem occurred in portrait orientation. I noticed that if I rotated the device to landscape mode, the date was drawn correctly. Moreover if I then rotated it back to portrait mode it then drew correctly.

I added the following two lines to my table view controller's viewDidLoad method:

    [self.tableView setNeedsLayout];

    [self.tableView layoutIfNeeded];

This fixed the problem - the date picker now draws correctly the first time in portrait mode.

1

React Native

These two things worked for me.

Add below code in didFinishLaunching function in appdelegate.m :

if (@available(iOS 14, *)) {
UIDatePicker *picker = [UIDatePicker appearance];
picker.preferredDatePickerStyle = UIDatePickerStyleWheels;
}

In DatePicker customStyles in JS File:

<DatePicker
customStyles={{
datePicker: {
backgroundColor: '#d1d3d8',
justifyContent:'center'
}
}}
...
/>
Achal Gandhi
  • 151
  • 7
0

UPDATE: Further investigation reveals that it is a problem with constraints trying to push the UIDateView in... Try braking the constraints and work it out from there... For sure it's a constraints issue.


Turn off "Clip SubViews" option/property of the UIDatePiker in the attributes inspector panel (under the view section) located to the right side in Storyboard editor of XCODE.

Attributes Inspector

If turning off this check mark does not work, try turning it off running the app and then turning it on again. I had have mixed results. But at the end it should work.

Kiko Lobo
  • 547
  • 5
  • 13
  • this does not seem to be the problem in my case. But thanks for your suggestion. Phillip – PhillipOReilly Nov 14 '14 at 15:05
  • Try removing all constraints and de-ticking or ticking this checkmark.. Run and try again... I tried a couple of times until it worked.. I can't seem to have a reproducible pattern to fix!! – Kiko Lobo Nov 15 '14 at 23:30
0

When I set the date picker to a custom date, my weekdays appeared.

Ken
  • 773
  • 1
  • 9
  • 17