Swift-如何自定义键盘(iOS)

//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) {

        

        //在此处做响应的处理

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值