Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker not displaying wheel for day of the month (dd) on iPad for iOS 8.1

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

like image 475
PhillipOReilly Avatar asked Oct 14 '14 04:10

PhillipOReilly


4 Answers

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.

like image 163
user3404693 Avatar answered Oct 22 '22 06:10

user3404693


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.

like image 42
Mike Avatar answered Oct 22 '22 06:10

Mike


try this

[myDatePicker setDatePickerMode:UIDatePickerModeDateAndTime];
[myDatePicker setDatePickerMode:UIDatePickerModeDate];
like image 21
user2885077 Avatar answered Oct 22 '22 07:10

user2885077


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.

like image 1
user3285526 Avatar answered Oct 22 '22 07:10

user3285526