How to set Minimum time to appear as default in UI

2019-01-20 05:29发布

In Datepicker i have set Min and Max time where uidatepicker can scroll for example i have set 3:00PM as minimum time and 8:00PM as maximum time , when i run the app i want the datepicker to display the Minimum time not the current time of the device . I have hard coded UIDatepicker in my project havent used in xib file . I have set Min time but when i switch from control.tag ==1 to 2 the minimum time changes and also when i open the app for first time the datepicker doesnt start from minimum time.

UPDATE :

-(IBAction)notificationButtonChanged:(UIButton *)control
{

    if (control.tag == 1)
    {
        self.selectedButton = control.tag;

        [userTimePicker reloadInputViews];

        [components setHour:7];;
        [components setMinute:00];

        NSDate *date1 = [gregorian dateFromComponents:components];

        [components setHour:11];
        [components setMinute:00];

        NSDate *date2 = [gregorian dateFromComponents:components];

        [userTimePicker setDatePickerMode:UIDatePickerModeTime];

        [userTimePicker setMinimumDate:date1];
        [userTimePicker setMaximumDate:date2];

    }

    if (control.tag == 2)
    {
        self.selectedButton = control.tag;

        [userTimePicker reloadInputViews];

        [components setHour:12];
        [components setMinute:00];

        NSDate *date1 = [gregorian dateFromComponents:components];

        [components setHour:17];
        [components setMinute:00];

        NSDate *date2 = [gregorian dateFromComponents:components];

        [userTimePicker setDatePickerMode:UIDatePickerModeTime];

        [userTimePicker setMinimumDate:date1];
        [userTimePicker setMaximumDate:date2];

    }

}

5条回答
来,给爷笑一个
2楼-- · 2019-01-20 05:46

Updated for Swift 3 :

        let startHour: Int = 6
        let endHour: Int = 23
        let date1: NSDate = NSDate()
        let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!

        let components: NSDateComponents = gregorian.components(([.day, .month, .year]), from: date1 as Date) as NSDateComponents
        components.hour = startHour
        components.minute = 0
        components.second = 0
        let startDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate

        components.hour = endHour
        components.minute = 0
        components.second = 0
        let endDate: NSDate = gregorian.date(from: components as DateComponents)! as NSDate

        timePicker.datePickerMode = .time
        timePicker.minimumDate = startDate as Date
        timePicker.maximumDate = endDate as Date
        timePicker.setDate(startDate as Date, animated: true)
        timePicker.reloadInputViews()
        timePicker.frame = CGRect(x: 0.0, y: (self.view.frame.height/2 + 60), width: self.view.frame.width, height: 150.0)
        timePicker.backgroundColor = UIColor.white
//        timePicker.reloadInputViews()
        self.view.addSubview(timePicker)
        timePicker.addTarget(self, action: #selector(RoundTripViewController.startTimeDiveChanged), for: UIControlEvents.valueChanged)
查看更多
走好不送
3楼-- · 2019-01-20 05:49

EDIT:

int startHour = 7;
int endHour = 11;

NSDate *date1 = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents *components = [gregorian components: NSUIntegerMax fromDate: date1];
[components setHour: startHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *startDate = [gregorian dateFromComponents: components];

[components setHour: endHour];
[components setMinute: 0];
[components setSecond: 0];
NSDate *endDate = [gregorian dateFromComponents: components];

[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:startDate];
[userTimePicker setMaximumDate:endDate];
[userTimePicker setDate:startDate animated:YES];
[userTimePicker reloadInputViews];
查看更多
虎瘦雄心在
4楼-- · 2019-01-20 05:53
datePickerObj = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, 100, 300, 160)];
datePickerObj.datePickerMode = UIDatePickerModeTime;
datePickerObj.backgroundColor = [UIColor clearColor];
[datePickerObj addTarget:self action:@selector(onDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged];

Pass String into method and format to date...

-(void)Set_MinimumDate_ToDatePicker:(NSString*)Time //  "start_time" = "3:00PM";
{
    NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
    [dformat setDateFormat:@"hh:mma"];

    [datePickerObj setMinimumDate:[dformat dateFromString:Time]];
    [datePickerObj setDate:[dformat dateFromString:Time] animated:YES];
    [datePickerObj reloadInputViews];
}

While change the Picker, it convert time to UTC+00:00, But it displays time to your time Zone eg. India, it add UTC+05:30 to the Time. If any TimeZone problem set TimeZone to the NSDateFormatter.

-(void)onDatePickerValueChanged:(UIDatePicker *)datePicker
{
    NSDateFormatter *dformat = [[NSDateFormatter alloc]init];
    [dformat setDateFormat:@"hh:mm a"];

    NSLog(@"%@",datePicker.date); //  2000-01-01 11:30:00 +0000
    [newStartBtn setTitle:[dformat stringFromDate:datePicker.date] forState:UIControlStateNormal]; 
    //"05:00 PM";
 }
查看更多
对你真心纯属浪费
5楼-- · 2019-01-20 05:56

The UIDatePicker class has a setDate:animated: method that you can use to set the date to whatever date/time you want on the date picker instance.

More info: http://developer.apple.com/library/ios/documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html#//apple_ref/doc/uid/TP40006806-CH3-SW1

查看更多
Explosion°爆炸
6楼-- · 2019-01-20 06:04

For Swift version:

let startHour: Int = 6
let endHour: Int = 23
let date1: NSDate = NSDate()
let gregorian: NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
let components: NSDateComponents = gregorian.components(([.Day, .Month, .Year]), fromDate: date1)
components.hour = startHour
components.minute = 0
components.second = 0
let startDate: NSDate = gregorian.dateFromComponents(components)!
components.hour = endHour
components.minute = 0
components.second = 0
let endDate: NSDate = gregorian.dateFromComponents(components)!
datePicker.datePickerMode = .Time
datePicker.minimumDate = startDate
datePicker.maximumDate = endDate
datePicker.setDate(startDate, animated: true)
datePicker.reloadInputViews()
查看更多
登录 后发表回答