datePicker未显示在所需的位置

时间:2018-08-10 05:05:00

标签: ios swift uiview uidatepicker

我创建了我的自定义datepicker视图,并在其中添加了UIdatePicker和两个按钮。 这两个按钮都在所需的正确位置,但日期选择器未显示在正确的位置

enter image description here

图像中的红色区域是日期选择器

这是我的日期选择器代码:

   @objc func datePicker(){

    //DatepickerView
    self.datePickerView.frame = CGRect(x: 5, y: Int(SCREEN_HEIGHT-280), width: Int(SCREEN_WIDTH-10), height: 276)
    self.datePickerView.backgroundColor = UIColor.white
    self.datePickerView.layer.cornerRadius = 8.0
    self.datePickerView.layer.masksToBounds = true
    self.datePickerView.layer.borderWidth = 2;
    self.datePickerView.layer.borderColor = UIColor.black.cgColor;

    doneBtn.addTarget(self, action: #selector(doneClicked), for: .touchUpInside)
    doneBtn.setTitle("Done", for: .normal)
    doneBtn.frame = CGRect(x: 20, y: 10, width: 80, height: 50)
    doneBtn.setTitleColor(UIColor.white, for: .normal)
    doneBtn.backgroundColor = UIColor.black        
   doneBtn.titleLabel?.font = UIFont(name: "Montserrat-Regular", size: 
   16.0)
    doneBtn.layer.cornerRadius = 16.0
    doneBtn.layer.masksToBounds = true
    doneBtn.layer.borderColor = UIColor.clear.cgColor
    doneBtn.layer.borderWidth = 1

    cancelBtn.addTarget(self, action:#selector(cancelClicked), for: .touchUpInside)
    cancelBtn.setTitle("Cancel",for: .normal)
    cancelBtn.frame = CGRect(x: SCREEN_WIDTH-110, y: 10, width: 80, height: 50)
    cancelBtn.setTitleColor(UIColor.white, for: .normal)
    cancelBtn.backgroundColor = UIColor.black
    cancelBtn.titleLabel?.font = UIFont(name: "Montserrat-Regular", size: 16.0)
    cancelBtn.layer.cornerRadius = 16.0
    cancelBtn.layer.masksToBounds = true
    cancelBtn.layer.borderColor = UIColor.clear.cgColor
    cancelBtn.layer.borderWidth = 1

    // DatePicker    
    self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: Int(SCREEN_WIDTH-20), height: 200))
    self.datePicker?.backgroundColor = UIColor.red
    self.datePicker?.datePickerMode = UIDatePickerMode.date
    self.datePicker?.layer.masksToBounds = true
    datePicker.center = view.center

        datePickerView.addSubview(datePicker)
        datePickerView.addSubview(doneBtn)
        datePickerView.addSubview(cancelBtn)

        self.view.addSubview(datePickerView)
        datePickerView.isHidden = true;
}

我在stackoverflow中尝试了10个以上的答案,但对我没有用。

任何想法,为什么它没有出现在正确的位置

3 个答案:

答案 0 :(得分:0)

您到底想实现什么?

根据您的代码:

// DatePicker    
self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: Int(SCREEN_WIDTH-20), height: 200))
self.datePicker?.backgroundColor = UIColor.red
self.datePicker?.datePickerMode = UIDatePickerMode.date
self.datePicker?.layer.masksToBounds = true
datePicker.center = view.center

您正在根据datePicker view设置self.datePickerView帧,并添加为subView。但同时您要添加:

datePicker.center = view.center

根据具有self.datePickerView作为subview的主视图。

datePicker在那种情况下超出范围。

您的datePicker y框架也不能为0。它将与donecancel button重叠。

答案 1 :(得分:0)

希望这会有所帮助:

我评论了日期选择器中心对齐代码:

        // DatePicker
        self.datePicker = UIDatePicker(frame:CGRect(x: Int(self.doneBtn.frame.origin.x) , y: Int(doneBtn.frame.origin.y + doneBtn.frame.size.height + 10), width: Int(cancelBtn.frame.origin.x + cancelBtn.frame.size.width - self.doneBtn.frame.origin.x), height: 200))
        self.datePicker.backgroundColor = UIColor.red
        self.datePicker.datePickerMode = UIDatePickerMode.date
        self.datePicker.layer.masksToBounds = true
//        datePicker.center = view.center 

enter image description here

答案 2 :(得分:0)

这是我遇到的 iOS 14 问题,我的 datePicker 只能从屏幕底部窥视。我从 PetterBraka 找到了答案 here

if #available(iOS 14, *) {
    datePicker.preferredDatePickerStyle = .wheels
    datePicker.sizeToFit()
}
yourTextField.inputView = datePicker