How to display UIView over keyboard in iOS

2019-01-14 19:01发布

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?

6条回答
等我变得足够好
2楼-- · 2019-01-14 19:04

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!

查看更多
乱世女痞
3楼-- · 2019-01-14 19:05

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.

查看更多
Lonely孤独者°
4楼-- · 2019-01-14 19:08

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)
查看更多
萌系小妹纸
5楼-- · 2019-01-14 19:10

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.

查看更多
▲ chillily
6楼-- · 2019-01-14 19:14

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);
}
查看更多
乱世女痞
7楼-- · 2019-01-14 19:21

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.

查看更多
登录 后发表回答