【问题标题】:UIDatepicker toolbar not working in landscape mode in ios swiftUIDatepicker工具栏在ios swift中无法在横向模式下工作
【发布时间】:2018-11-26 06:22:07
【问题描述】:

适用于横向和纵向的应用程序。使用 UIDatepicker 选择日期。这里是UIDatepicker代码。

class ViewController: UIViewController {
    //Text Field Connection
    @IBOutlet weak var txtDatePicker: UITextField!
    //Uidate picker
    let datePicker = UIDatePicker()

    override func viewDidLoad() {
        super.viewDidLoad()
        //show date picker
        showDatePicker()
    }
    func showDatePicker(){
        //Formate Date
        datePicker.datePickerMode = .date

        //ToolBar
        let toolbar = UIToolbar();
        toolbar.sizeToFit()

        //done button & cancel button
        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.bordered, target: self, action: "donedatePicker")
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.bordered, target: self, action: "cancelDatePicker")
        toolbar.setItems([doneButton,spaceButton,cancelButton], animated: false)

        // add toolbar to textField
        txtDatePicker.inputAccessoryView = toolbar
        // add datepicker to textField
        txtDatePicker.inputView = datePicker

    }
    func donedatePicker(){
        //For date formate
        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MM/yyyy"
        txtDatePicker.text = formatter.string(from: datePicker.date)
        //dismiss date picker dialog
        self.view.endEditing(true)
    }
    func cancelDatePicker(){
        //cancel button dismiss datepicker dialog
        self.view.endEditing(true)
    }
}

UIDatePicker 工具栏在纵向模式下工作正常。

在横向模式下更改应用程序工具栏隐藏。

如何解决此问题的帮助。

谷歌它并参考一些链接:

Add a Done button within a pop-up datePickerView in Swift?

它不会对我锻炼。帮我解决这个问题。

【问题讨论】:

  • 我认为你需要设置日期选择器的框架大小,即 self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width , 高度: 216))
  • 更新我的问题的答案。

标签: ios iphone swift toolbar uidatepicker


【解决方案1】:

我认为您需要为 datepicker 设置框架。

self.datePicker = UIDatePicker(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))

【讨论】:

    【解决方案2】:

    这似乎是iOS 11 中的一个错误,并且没有出现在iOS 12 中。请在苹果开发者论坛中参考。

    https://forums.developer.apple.com/thread/81650

    修复:

    func textFieldDidBeginEditing(_ textField: UITextField) {
    
        let item = textField.inputAssistantItem
        item.leadingBarButtonGroups = []
        item.trailingBarButtonGroups = []
    
        //Your code here....
    }
    

    也请看一下这个链接一次

    Custom Keyboard InputAccessoryView not visible in iOS 11

    【讨论】:

    • 如何实现此代码来解决我的问题..帮助我在这里挣扎,
    • 在你的 UITextFieldDelegate 方法中添加上面的代码
    • 尝试将工具栏设为全局并设置toolbar.autoresizingMask = .flexibleHeight
    • @saravanar 在情节提要中添加日期选择器,并设置参考。我在我的项目中尝试了您上面的代码并且它运行良好。唯一的区别是我在情节提要中添加了日期选择器。
    • 感谢@kalpa 提供的信息。请尝试在情节提要中添加 datePicker 并尝试一次,saravanar。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多