标题:
iOS中实现键盘右上角完成按钮
[打印本页]
作者:
look_w
时间:
2019-2-18 20:39
标题:
iOS中实现键盘右上角完成按钮
重写UITextField子类的drawRect方法
class UITextFieldX: UITextField {
override func drawRect(rect: CGRect) {
super.drawRect(rect)
// 键盘完成按钮
var toolBar = UIToolbar(frame: CGRectMake(0, 0, screenSize().width, 30))
toolBar.barStyle = UIBarStyle.Default
var btnFished = UIButton(frame: CGRectMake(0, 0, 50, 25))
btnFished.setTitleColor(RGB(4, 170, 174), forState: UIControlState.Normal)
btnFished.setTitleColor(UIColor.grayColor(), forState: UIControlState.Highlighted)
btnFished.setTitle("完成", forState: UIControlState.Normal)
btnFished.addTarget(self, action: "finishTapped:", forControlEvents: UIControlEvents.TouchUpInside)
var item2 = UIBarButtonItem(customView: btnFished)
var space = UIView(frame: CGRectMake(0, 0, screenSize().width - btnFished.frame.width - 30, 25))
var item = UIBarButtonItem(customView: space)
toolBar.setItems([item,item2], animated: true)
self.inputAccessoryView = toolBar
}
func finishTapped(sender:UIButton){
self.resignFirstResponder()
}
}
func screenSize() -> CGSize{
return UIScreen.mainScreen().bounds.size
}
func RGB (r:CGFloat, g:CGFloat, b:CGFloat) -> UIColor {
return UIColor(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
}
效果如下
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0