如何在数字键盘上添加小数点?
当我使用通知UIKeyboardDidShowNotification时,我希望一个UIButton出现在数字键盘的下方左侧。
这将是按钮的框架:
编辑: 我成功让按钮出现在数字键盘下方左侧。
这将是按钮的框架:
let dotButton = UIButton(frame: CGRectMake(0, 162, view.frame.width/3, 54))
,
但它必须作为数字键盘的子视图添加,以防止被其遮挡。该如何实现?编辑: 我成功让按钮出现在数字键盘下方左侧。
dotButton.addTarget(self, action: "addDot:", forControlEvents: .TouchUpInside)
dotButton.setTitle(".", forState: .Normal)
dotButton.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 25)
dotButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
dispatch_async(dispatch_get_main_queue(), {
let keyboardView:UIView = UIApplication.sharedApplication().windows.last?.subviews.first as! UIView
self.dotButton.frame = CGRectMake(0, keyboardView.frame.size.height-54, self.view.frame.width/3, 54)
keyboardView.addSubview(self.dotButton)
keyboardView.bringSubviewToFront(self.dotButton)
})
现在我不知道如何当我点击按钮调用addDot方法时,告诉textField添加一个.。需要再次寻求帮助...
编辑2:
我创建了一个类变量 textFieldInEdit:UITextField!
并在 func textFieldShouldBeginEditing(textField: UITextField) -> Bool
中执行 textFieldInEdit = textField
,现在我的 addDot
函数是这样的:
textFieldWhichEdit.text = "\(textFieldWhichEdit.text)."
,
但是我现在又遇到另一个问题...我有不同键盘类型的字段,如何检测并仅在数字键盘上显示点号?
编辑3:
func textFieldDidBeginEditing(textField: UITextField) {
if(textField.keyboardType.rawValue==4){
textFieldWhichEdit = textField
dotButton.hidden = false
}else{
dotButton.hidden = true
}
}
完成了!我在数字键盘上加了一个点按钮 :) 如果有更好的方法,可以写下来,我会接受这个答案 :)