开发者

UIKeyBoard Return Button UIReturnKeyDone

textfield.returnKeyTYpe = UIReturnKeyDone

So the above makes my Return button on the keyboard to say Done. I have seen Apps with Blue color button on the UIKeyBoard. Is that simple enough to do? How do I 开发者_开发百科change the background color of the Return key?


The Return key only turns blue if it has a type other than UIReturnKeyDefault and is enabled. To make sure it gets enabled, you can set textfield.enablesReturnKeyAutomatically = YES.


textfield.returnKeyType = UIReturnKeyGo;


I'm not entirely certain you can, and if you can I think that you might need to either roll your own keyboard or use undocumented methods. Alternatively, you could draw your own view over the button and simply make it transparent to user touches. This article might help you.


The return key background color turns AUTOMATICALLY into blue as soon as the user enters code for e.g. into a UITextField.


Here's the code in Swift that does the job:

extension ViewController {

func subviewsOfView(view : UIView, withType type:NSString) -> NSArray {
    let prefix = NSString(format: "<%@",type) as String
    let subViewsArray = NSMutableArray()
    for subview in view.subviews {
        let tempArray = subviewsOfView(subview, withType: type)
        for view in tempArray {
            subViewsArray.addObject(view)
        }
    }
    if view.description.hasPrefix(prefix) {
        subViewsArray.addObject(view)
    }
    return NSArray(array: subViewsArray)
}

func addColorToUIKeyboardButton() {
    for keyboardWindow in UIApplication.sharedApplication().windows {
        for keyboard in keyboardWindow.subviews {
            for view in self.subviewsOfView(keyboard, withType: "UIKBKeyplaneView") {
                let newView = UIView(frame: (self.subviewsOfView(keyboard, withType: "UIKBKeyView").lastObject as! UIView).frame)
                newView.frame = CGRectMake(newView.frame.origin.x + 2, newView.frame.origin.y + 1, newView.frame.size.width - 4, newView.frame.size.height - 3)
                newView.backgroundColor = UIColor.redColor() //Or whatever color you want
                newView.layer.cornerRadius = 4.0
                view.insertSubview(newView, belowSubview: self.subviewsOfView(keyboard, withType: "UIKBKeyView").lastObject as! UIView)
            }
        }
    }
}
}

Note: Make sure the keyboard is visible before you call addColorToUIKeyboardButton()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜