你可以将这个新的子视图添加到你的应用程序窗口中。
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);
}
Swift 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)
customView
添加为顶部子视图到持有键盘的 UIWindow
上 - 它恰好是在 UIApplication.shared.windows
中的最后一个窗口。windows
而不是 keyWindow
是可行的! - Tai LeSwift 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)
IBOutlet private weak var toolBar: UIView!
2) 对于你想要在键盘上方添加自定义视图的文本框,可以在viewDidLoad方法中将其添加为附加视图:
override func viewDidLoad() {
super.viewDidLoad()
phoneNumberTextField.inputAccessoryView = toolBar
}
3) 为 "下一步" 按钮添加动作:
@IBAction func nextButtonPressed(_ sender: Any) {
descriptionTextView.becomeFirstResponder()
// or -> phoneNumberTextField.resignFirstResponder()
}
解释图片:
方法2:使用图片的结果
在表视图控制器中-在底部添加粘滞视图
如果您想使用此(第二种)方法,请按照此重要链接处理iPhone X等屏幕的安全区域。文章:InputAccessoryView和iPhone X
override var inputAccessoryView: UIView? {
return toolBar
}
override var canBecomeFirstResponder: Bool {
return true
}
你有找到一些有效的方法来解决这个问题吗?在iOS9中,你可以把自定义视图放在窗口的顶部:
UIApplication.sharedApplication().windows[windowCount-1].addSubview(customView);
但是如果键盘消失,顶部的窗口也会被移除,所以你的customView
也会被移除。
期待你的帮助!
谢谢你的帮助!
您可以将该视图添加到应用程序的窗口中,也可以完全添加另一个窗口。 您可以设置其框架和级别。 级别可以是UIWindowLevelAlert
。
虽然可以通过访问最顶层的窗口来实现,但我建议避免这样做,因为它明显会干扰苹果的指南。
我会关闭键盘并用相同尺寸的视图替换其框架。
可以从键盘通知中访问键盘的框架,这些通知在此处列出,它们的userInfo
包含一个可以使用UIKeyboardFrameEndUserInfoKey
访问的键。
sender.window
代替UIApplication.sharedApplication().windows[...]
吗? - rintaro