如何在iOS中通过键盘显示UIView

我想通过键盘创build一个简单的视图,当用户点击inputAccessoryView中的“附加”button。 像这样的东西:

在这里输入图像说明

有一个简单的方法来做到这一点? 或者我应该创build我的自定义键盘?

您可以将新的子视图添加到您的应用程序窗口。

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

你有没有find一些有效的方法来解决这个问题? 在iOS9中,您将自定义视图放置在窗口顶部:UIApplication.sharedApplication()。windows [windowCount-1] .addSubview(customView); 但是,如果键盘消失,顶部Windows将被删除,所以你的customView将被删除。 期待您的帮助! 感谢您的帮助!

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) 

虽然这可以通过访问最上面的窗口来实现,但我会避免这样做,因为它明显干扰了苹果的指导方针。

我要做的就是解散键盘,并用相同尺寸的视图replace它的框架。

键盘的框架可以通过这里列出的键盘通知来访问,它们的userInfo包含一个可以用UIKeyboardFrameEndUserInfoKey访问的UIKeyboardFrameEndUserInfoKey