Skip to content

Instantly share code, notes, and snippets.

@feighter09
Last active July 25, 2022 06:10
Show Gist options
  • Save feighter09/d87dccf5bc2a3d7db0dd to your computer and use it in GitHub Desktop.
Save feighter09/d87dccf5bc2a3d7db0dd to your computer and use it in GitHub Desktop.
Change the keyboard "Done" button color to match the rest of your app! This is a total hack though and is likely to get flagged during app review. If so, try obfuscating =)
class Utilities {
class func subviewsOfView(view: UIView, withType type: String) -> [UIView]
{
let prefix = "<\(type)"
var subviewArray = view.subviews.flatMap { subview in subviewsOfView(subview, withType: type) }
if view.description.hasPrefix(prefix) {
subviewArray.append(view)
}
return subviewArray
}
}
private func changeDoneButtonColorWhenKeyboardShows()
{
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardWillShowNotification, object: nil, queue: nil) { (notification) -> Void in
self.changeKeyboardDoneKeyColor()
}
}
private func changeKeyboardDoneKeyColor()
{
let (keyboard, keys) = getKeyboardAndKeys()
for key in keys {
if keyIsOnBottomRightEdge(key, keyboardView: keyboard) {
let newButton = newDoneButtonWithOld(key)
keyboard.addSubview(newButton)
}
}
}
private func getKeyboardAndKeys() -> (keyboard: UIView, keys: [UIView])!
{
for keyboardWindow in UIApplication.sharedApplication().windows {
for view in keyboardWindow.subviews {
for keyboard in Utilities.subviewsOfView(view, withType: "UIKBKeyplaneView") {
let keys = Utilities.subviewsOfView(keyboard, withType: "UIKBKeyView")
return (keyboard, keys)
}
}
}
return nil
}
private func keyIsOnBottomRightEdge(key: UIView, keyboardView: UIView) -> Bool
{
let margin: CGFloat = 5
let onRightEdge = key.frame.origin.x + key.frame.width + margin > keyboardView.frame.width
let onBottom = key.frame.origin.y + key.frame.height + margin > keyboardView.frame.height
return onRightEdge && onBottom
}
private func newDoneButtonWithOld(oldButton: UIView) -> UIButton
{
let oldFrame = oldButton.frame
let newFrame = CGRect(x: oldFrame.origin.x + 2,
y: oldFrame.origin.y + 1,
width: oldFrame.size.width - 4,
height: oldFrame.size.height - 4)
let newButton = UIButton(frame: newFrame)
newButton.backgroundColor = .secondaryColor
newButton.layer.cornerRadius = 4;
newButton.setTitle("Done", forState: .Normal)
newButton.addTarget(self.searchBar, action: "resignFirstResponder", forControlEvents: .TouchUpInside)
return newButton
}
@feighter09
Copy link
Author

Sorry, didn't realize people would find this organically. When I posted this on FB I mentioned that this is likely to get flagged during review. I'll update the description

@rogerluan
Copy link

It is findable organically 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment