datePicker 未显示在所需位置
datePicker is not showing on desired place
我已经创建了自定义日期选择器视图并在其中添加了 UIdatePicker 和两个按钮。
两个按钮都按需要位于正确的位置,但日期选择器未显示在正确的位置
图中红色区域为日期选择器
这是我的日期选择器代码:
@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;
}
我在 Whosebug 中尝试了 10 多个答案,但对我没有用。
知道为什么它没有出现在正确的位置
你到底想达到什么目的?
根据您的代码:
// 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
您正在根据 view
self.datePickerView
设置 datePicker
框架并将其添加为 subView
。但同时你在添加:
datePicker.center = view.center
根据 self.datePickerView
作为 subview
的主视图。
datePicker
超出了该场景的范围。
另外你的datePicker
y frame不能为0。它会重叠done
和cancel
button
.
希望这会有所帮助:
我评论了日期选择器中心对齐代码:
// 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
这是我 运行 遇到的一个 iOS 14 问题,我的 datePicker 只是从屏幕底部向外窥视。我从 PetterBraka 找到了答案here:
if #available(iOS 14, *) {
datePicker.preferredDatePickerStyle = .wheels
datePicker.sizeToFit()
}
yourTextField.inputView = datePicker
我已经创建了自定义日期选择器视图并在其中添加了 UIdatePicker 和两个按钮。 两个按钮都按需要位于正确的位置,但日期选择器未显示在正确的位置
图中红色区域为日期选择器
这是我的日期选择器代码:
@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;
}
我在 Whosebug 中尝试了 10 多个答案,但对我没有用。
知道为什么它没有出现在正确的位置
你到底想达到什么目的?
根据您的代码:
// 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
您正在根据 view
self.datePickerView
设置 datePicker
框架并将其添加为 subView
。但同时你在添加:
datePicker.center = view.center
根据 self.datePickerView
作为 subview
的主视图。
datePicker
超出了该场景的范围。
另外你的datePicker
y frame不能为0。它会重叠done
和cancel
button
.
希望这会有所帮助:
我评论了日期选择器中心对齐代码:
// 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
这是我 运行 遇到的一个 iOS 14 问题,我的 datePicker 只是从屏幕底部向外窥视。我从 PetterBraka 找到了答案here:
if #available(iOS 14, *) {
datePicker.preferredDatePickerStyle = .wheels
datePicker.sizeToFit()
}
yourTextField.inputView = datePicker