Swift-如何自定义键盘(iOS)
2016-09-21 13:46
211 查看
//createMyInputView该方法返回一个UIImageView类型的视图
let myView =
createMyInputView();
//让此视图作为键盘的背景视图
textField.inputView = myView
//添加附件区域
let upView =
UIView.init(frame:
CGRectMake(0,
0, self.view.frame.size.width,
60))
//附件区域的颜色
upView.backgroundColor =
UIColor.grayColor()
//让upView作为附件区域
textField.inputAccessoryView = upView
//在附件区域中添加“确定”按钮
let sendButton =
UIButton.init(type:
UIButtonType.System)
sendButton.frame =
CGRectMake(self.view.frame.size.width-80,
6, 80,
40)
sendButton.setTitle("确定",
forState: UIControlState.Normal)
upView.addSubview(sendButton)
//自定义键盘
func createMyInputView() ->
UIImageView {
//创建一个背景ImageView
let inputView =
UIImageView.init(frame:
CGRectMake(0,
0, self.view.frame.size.width,
200))
//添加背景图片
inputView.image =
UIImage(named:"DOVE 1")
//打开用户交互
inputView.userInteractionEnabled =
true
//视图的背景色
inputView.backgroundColor =
UIColor.init(white:
1, alpha: 0.5)
//添加按钮
let titleArray = ["京","津","追","梦","人","自","定","义","键","盘","一","二","三","四","五","六","七","八","九","十","取钱","红包","收"]
//有多少个字就创建多少个按钮
for i
in 0..<titleArray.count {
//创建按钮
let button =
UIButton.init(type:
UIButtonType.System)
//按钮的坐标
button.frame =
CGRectMake(40*CGFloat(i%10),
CGFloat((i)/10)*40,
40, 40)
//按钮上显示的文字
button.setTitle(titleArray[i], forState:
UIControlState.Normal)
//把创建的每一个按钮添加到inputView上
inputView.addSubview(button)
//设置按钮的tag值
button.tag = i+1
//给每个按钮添加点击事件
button.addTarget(self, action:
#selector(btnClick(_:)), forControlEvents:
UIControlEvents.TouchUpInside)
}
//返回背景视图
return inputView;
}
//按钮的点击事件
func btnClick(button:UIButton) {
//在此处做响应的处理
}
let myView =
createMyInputView();
//让此视图作为键盘的背景视图
textField.inputView = myView
//添加附件区域
let upView =
UIView.init(frame:
CGRectMake(0,
0, self.view.frame.size.width,
60))
//附件区域的颜色
upView.backgroundColor =
UIColor.grayColor()
//让upView作为附件区域
textField.inputAccessoryView = upView
//在附件区域中添加“确定”按钮
let sendButton =
UIButton.init(type:
UIButtonType.System)
sendButton.frame =
CGRectMake(self.view.frame.size.width-80,
6, 80,
40)
sendButton.setTitle("确定",
forState: UIControlState.Normal)
upView.addSubview(sendButton)
//自定义键盘
func createMyInputView() ->
UIImageView {
//创建一个背景ImageView
let inputView =
UIImageView.init(frame:
CGRectMake(0,
0, self.view.frame.size.width,
200))
//添加背景图片
inputView.image =
UIImage(named:"DOVE 1")
//打开用户交互
inputView.userInteractionEnabled =
true
//视图的背景色
inputView.backgroundColor =
UIColor.init(white:
1, alpha: 0.5)
//添加按钮
let titleArray = ["京","津","追","梦","人","自","定","义","键","盘","一","二","三","四","五","六","七","八","九","十","取钱","红包","收"]
//有多少个字就创建多少个按钮
for i
in 0..<titleArray.count {
//创建按钮
let button =
UIButton.init(type:
UIButtonType.System)
//按钮的坐标
button.frame =
CGRectMake(40*CGFloat(i%10),
CGFloat((i)/10)*40,
40, 40)
//按钮上显示的文字
button.setTitle(titleArray[i], forState:
UIControlState.Normal)
//把创建的每一个按钮添加到inputView上
inputView.addSubview(button)
//设置按钮的tag值
button.tag = i+1
//给每个按钮添加点击事件
button.addTarget(self, action:
#selector(btnClick(_:)), forControlEvents:
UIControlEvents.TouchUpInside)
}
//返回背景视图
return inputView;
}
//按钮的点击事件
func btnClick(button:UIButton) {
//在此处做响应的处理
}
相关文章推荐
- Objective-C-如何自定义键盘(iOS)
- ios中如何自定义数字键盘
- Swift技术之如何在iOS 8下使用Swift设计一个自定义的输入法 (主要是NSLayoutConstraint 的使用)
- IOS swift 3.0 关于自定义键盘附件关闭虚拟键盘
- iOS开发之如何自定义UIActionSheet(UIAlertView)中的内容
- iOS中如何添加自定义的字体
- IOS开发之自定义系统弹出键盘上方的view
- 如何在Ubuntu中自定义键盘快捷[译]
- IOS开发 当键盘出现的时候,如何让UITextField自动上移
- IOS开发 ,当键盘出现的时候如何让UITextField自动上移
- IOS开发之自定义系统弹出键盘上方的view(转载)
- iOS开发笔记--如何自定义启动画面Default.png加载时间
- 如何把自定义的字体添加到 iOS 应用中?
- 【ios】论自定义的控件如何在xib中使用,自定义控件在xib中的初始化入口函数
- 如何保持iOS上键盘出现时输入框不被覆盖
- Hack the UIKeyboard(自定义iOS内置的键盘)
- 如何在Photoshop中自定义键盘快捷键
- 为数字键盘添加自定义按钮的方法(iOS所有版本适用
- ios如何添加自定义字体
- iOS ShardSDK-如何自定义分享菜单中的平台列表