gpt4 book ai didi

ios - 自定义 uiview 作为 inputview swift

转载 作者:行者123 更新时间:2023-11-30 13:18:31 25 4
gpt4 key购买 nike

我有一个自定义输入 View :

func datePickerInputView() {
let screenWidth = UIScreen.mainScreen().bounds.width
let dateView = UIView(frame: CGRectMake(100, 100, screenWidth, 160))
datePicker = UIDatePicker(frame: CGRectMake(0, 0, screenWidth, 160))
datePicker.datePickerMode = .Date
datePicker.backgroundColor = UIColor.whiteColor()
dateView.addSubview(datePicker)
inputTextField.inputView = dateView
let inputAccView = UIView(frame: CGRectMake(100, 100, 100, 88))
let topMessage = UILabel(frame: CGRectMake(0, 0, screenWidth, 44))
topMessage.textAlignment = .Center
topMessage.text = "My Custom datePicker Keyboard"
topMessage.backgroundColor = UIColor.blackColor()
topMessage.textColor = UIColor(netHex: 0xFFAE00)
topMessage.font = UIFont.boldSystemFontOfSize(17)
topMessage.adjustsFontSizeToFitWidth = true
inputAccView.addSubview(topMessage)
let doneButton = UIButton (frame: CGRectMake(0, 45, (screenWidth / 2) - 1, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "dateUpdated", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor.darkGrayColor()
inputAccView.addSubview(doneButton)// = doneButton
let cancelButton = UIButton (frame: CGRectMake((screenWidth / 2) + 1, 45, (screenWidth / 2) - 1, 44))
cancelButton.setTitle("Cancel", forState: UIControlState.Normal)
cancelButton.addTarget(self, action: "didTapView", forControlEvents: UIControlEvents.TouchUpInside)
cancelButton.backgroundColor = UIColor.darkGrayColor()
inputAccView.addSubview(cancelButton)// = cancelButton
let whitStripe = UIView(frame: CGRectMake((screenWidth / 2) - 1, 48, 2, 36))
whitStripe.backgroundColor = UIColor.whiteColor()
inputAccView.addSubview(whitStripe)
inputTextField.inputAccessoryView = inputAccView
}

inputTextField 和 datePicker 在同一个 View Controller 中声明,当我希望显示此 datePickerView 时,我只需运行以下两个代码:

self.datePickerInputView()
self.inputTextField.becomeFirstResponder()

doneButton 的函数是:

func dateUpdated() {
//Run required code
inputTextField.resignFirstResponder()
}

但在我看来,我并没有真正使用 inputTextField 。我把它藏起来了。但当我单击按钮时,我需要它调用 inputView,因为我找不到为按钮分配 inputview 的方法。

我在所有想要用 datePicker 显示这种类型的键盘的 View Controller 中实现了这个。

但是我想创建一个单独的 UIView 子类来实现这个键盘 View ,并在我想要显示这个键盘布局时调用它,而不是在每个需要它的 viewController 中执行此操作。

我寻找自定义键盘,但我发现的大多数键盘都实现了 xib。

我是否可以有一个 UIView 的子类,它可以在需要时调用的函数 datePickerView() 中按照上面的方式进行输入 View 的布局?

谢谢

最佳答案

这是你想要的吗?

extension UIViewController {
func datePickerInputView(inout datePicker: UIDatePicker, inputTextField: UITextField) {
let screenWidth = UIScreen.mainScreen().bounds.width
let dateView = UIView(frame: CGRectMake(100, 100, screenWidth, 160))
datePicker = UIDatePicker(frame: CGRectMake(0, 0, screenWidth, 160))
datePicker.datePickerMode = .Date
datePicker.backgroundColor = UIColor.whiteColor()
dateView.addSubview(datePicker)
inputTextField.inputView = dateView
let inputAccView = UIView(frame: CGRectMake(100, 100, 100, 88))
let topMessage = UILabel(frame: CGRectMake(0, 0, screenWidth, 44))
topMessage.textAlignment = .Center
topMessage.text = "My Custom datePicker Keyboard"
topMessage.backgroundColor = UIColor.blackColor()
topMessage.textColor = UIColor(netHex: 0xFFAE00)
topMessage.font = UIFont.boldSystemFontOfSize(17)
topMessage.adjustsFontSizeToFitWidth = true
inputAccView.addSubview(topMessage)
let doneButton = UIButton (frame: CGRectMake(0, 45, (screenWidth / 2) - 1, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "dateUpdated", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor.darkGrayColor()
inputAccView.addSubview(doneButton)// = doneButton
let cancelButton = UIButton (frame: CGRectMake((screenWidth / 2) + 1, 45, (screenWidth / 2) - 1, 44))
cancelButton.setTitle("Cancel", forState: UIControlState.Normal)
cancelButton.addTarget(self, action: "didTapView", forControlEvents: UIControlEvents.TouchUpInside)
cancelButton.backgroundColor = UIColor.darkGrayColor()
inputAccView.addSubview(cancelButton)// = cancelButton
let whitStripe = UIView(frame: CGRectMake((screenWidth / 2) - 1, 48, 2, 36))
whitStripe.backgroundColor = UIColor.whiteColor()
inputAccView.addSubview(whitStripe)
inputTextField.inputAccessoryView = inputAccView
}
}

关于ios - 自定义 uiview 作为 inputview swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38003442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com