How to display UIView over keyboard in iOS
Asked Answered
F

7

16

I want to create a simple view over keyboard, when users tap "Attach" button in inputAccessoryView. Something like this:

enter image description here

Is there an easy way to do it? Or i should create my custom keyboard?

Fiftieth answered 4/9, 2015 at 6:45 Comment(0)
R
22

You can add that new subview to your application window.

func attach(sender : UIButton)
{
    // Calculate and replace the frame according to your keyboard frame
    var customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height-300, width: self.view.frame.size.width, height: 300))
    customView.backgroundColor = UIColor.redColor()
    customView.layer.zPosition = CGFloat(MAXFLOAT)
    var windowCount = UIApplication.sharedApplication().windows.count
    UIApplication.sharedApplication().windows[windowCount-1].addSubview(customView);
}
Rist answered 4/9, 2015 at 7:1 Comment(4)
You could use sender.window instead of UIApplication.sharedApplication().windows[...]?Flirtation
@rintaro: That won't work, that will add the view below the keyboardRist
This is a hacky solution and should be avoided. The window hierarchy can change on any newly introduced iOS version.Miscall
this one not support for ios 16 , cannot add view over keybbaord, anything to fixedthis ?Fourscore
F
10

Swift 4 version:

let customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height - 300, width: self.view.frame.size.width, height: 300))
customView.backgroundColor = UIColor.red
customView.layer.zPosition = CGFloat(Float.greatestFiniteMagnitude)
UIApplication.shared.windows.last?.addSubview(customView)

The trick is to add the customView as a top subview to the UIWindow that holds the keyboard - and it happens to be the last window in UIApplication.shared.windows.

Fremantle answered 1/2, 2018 at 14:30 Comment(2)
Getting last of windows rather than keyWindow from UIApplication works!Dietrich
Hey, this code doesn't work with Xcode 14.Mica
L
5

Swift 4.0

let customView = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height-300, width: self.view.frame.size.width, height: 300))
customView.backgroundColor = UIColor.red
customView.layer.zPosition = CGFloat(MAXFLOAT)
let windowCount = UIApplication.shared.windows.count
UIApplication.shared.windows[windowCount-1].addSubview(customView)
Lintel answered 27/10, 2017 at 16:35 Comment(0)
J
4

As Tamás Sengel said, Apple's guidelines does not support adding a view over the keyboard. The recommended way to add a view over keyboard in Swift 4 & 5 is:

1) Add view with your "Next" button in your storyboard as external view and connect in your class (see Explain Image), in my case:

IBOutlet private weak var toolBar: UIView!

2) For the textfield you want to add your custom view over keyboard, add it as accessory view in viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    phoneNumberTextField.inputAccessoryView = toolBar
}

3) Add action for "Next" button:

@IBAction func nextButtonPressed(_ sender: Any) {
    descriptionTextView.becomeFirstResponder()

    // or -> phoneNumberTextField.resignFirstResponder()
}

Explain Image: enter image description here

Method 2: Result with image

enter image description here

In TableView Controller - add stricked view at bottom

Please follow this great link to handle safe area for screens like iPhone X if you want to use this method(2). Article: InputAccessoryView and iPhone X

override var inputAccessoryView: UIView? {
    return toolBar
}

override var canBecomeFirstResponder: Bool {
    return true
}
Judgemade answered 11/3, 2019 at 14:39 Comment(1)
This should be the correct answer. Thanks user doca. You are the man.Mckeown
W
3

Do you have find some effective method to solve this problem? In iOS9,you put your customView on the top of the windows:

UIApplication.sharedApplication().windows[windowCount-1].addSubview(customView);

But if the keyboard dismisses, the top Windows will be removed, so your customView will be removed. Looking forward for your help! Thank you for your help!

Whereunto answered 22/9, 2015 at 10:16 Comment(1)
Please see my answer, i solve this problem like this - #32598990Fiftieth
B
2

You can definitely add the view to your application’s window, and you can also add another window entirely. You can set its frame and level. The level could be UIWindowLevelAlert.

Boutonniere answered 1/2, 2018 at 15:57 Comment(0)
J
0

While this can be possible with accessing the topmost window, I would avoid doing this, as it clearly interferes with Apple's guidelines.

What I would do is dismissing the keyboard and replacing its frame with a view with same dimensions.

The keyboard's frame can be accessed from keyboard notifications listed here, their userInfo contain a key that can be accessed with UIKeyboardFrameEndUserInfoKey.

Jingoism answered 24/8, 2017 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.