码迷,mamicode.com
首页 > 移动开发 > 详细

iOS中实现键盘右上角完成按钮

时间:2015-10-08 16:03:46      阅读:651      评论:0      收藏:0      [点我收藏+]

标签:

import UIKit

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)
}

 

效果如下:

技术分享

iOS中实现键盘右上角完成按钮

标签:

原文地址:http://www.cnblogs.com/lancely/p/4861066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!