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

pav*_*l_s 10 uiview uikeyboard ios swift

当用户点击inputAccessoryView中的"附加"按钮时,我想在键盘上创建一个简单的视图.像这样的东西:

在此输入图像描述

有一个简单的方法吗?或者我应该创建我的自定义键盘?

Mid*_* MP 14

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

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);
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个hacky解决方案,应该避免.窗口层次结构可以在任何新引入的iOS版本上更改. (3认同)

Mil*_*sáľ 7

斯威夫特 4 版本:

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)
Run Code Online (Sandbox Code Playgroud)

诀窍是将customView作为顶部子视图添加到UIWindow持有键盘的 中 - 它恰好是UIApplication.shared.windows.


Abh*_*ain 5

迅捷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)
Run Code Online (Sandbox Code Playgroud)