Loading a UIDatePicker in Landscape on a UINavigationController

前端 未结 6 662
慢半拍i
慢半拍i 2021-02-14 20:44

I have a custom UINavigationController that supports auto-rotate. I also have a UIDatePicker on one of my views that I throw onto the stack of the Navigation controller. The a

6条回答
  •  暖寄归人
    2021-02-14 21:02

    The below code will work for both portrait and Landscape mode. For this you need to set your tableview frame origin like that below:-

    _dateTableViewCell=[self.tableView dequeueReusableCellWithIdentifier:@"DatePickerCell"];
    if (self.dateTableViewCell==nil)
    {
        CGRect rect;
        self.dateTableViewCell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"DatePickerCell"];
        UIDatePicker *datePicker=[[UIDatePicker alloc]initWithFrame:frame];
        datePicker.datePickerMode=UIDatePickerModeDateAndTime;
        datePicker.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        CGRect cellRect = self.dateTableViewCell.frame;
        cellRect.size.height = 216;
        self.dateTableViewCell.frame = cellRect;
        CGRect startRect = self.dateTableViewCell.frame;
        startRect.origin.x = 0.0;
        datePicker.frame = startRect;
        [self.dateTableViewCell.contentView addSubview:datePicker];
    }
    

提交回复
热议问题