iOS中使用UIDatePicker制作时间选择器的实例教程

2020-01-15 14:40:24于海丽


实例
而今天我们要做的时间选取器成品具体效果如下:

UIDatePicker,iOS,时间选择器

我们自定义一个LGDatePickerView,在LGDatePickerView里面实现。

背景半透明:

背景是半透明的,点击的灰色背景的时候,时间选取器消失。在LGDatePickerView初始化方法里,代码如下:

复制代码
- (id)init
{
    self = [super init];
    if (self) {
  //背景半透明,绑定取消方法
    UIControl *control = [[UIControl alloc] initWithFrame:SCREEN_BOUNDS];
    control.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.5f];
    [self addSubview:control];
    [control addTarget:self action:@selector(actionCancel:) forControlEvents:UIControlEventTouchUpInside];     
     }
    return self;
}
绑定的actionCancel方法:
复制代码
- (void)actionCancel:(id)sender
{
    [self removeFromSuperview];
}
确定取消按钮:

 

看到上面的确定取消按钮,你会怎么做,写一个UIView上面放两个UIButton。这样做也是可以实现的。我们还可以用UIToolbar。在LGDatePickerView初始化方法里加上下面这段代码:

复制代码
 // Toolbar