2015-09-04 31 views
7

当用户点击inputAccessoryView中的“附加”按钮时,我想通过键盘创建一个简单视图。 事情是这样的:如何在iOS中通过键盘显示UIView

enter image description here

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

回答

9

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

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

是的,这正是我需要的,非常感谢! –

+0

@pavel_s:不客气。快乐编码! –

+0

你可以使用'sender.window'而不是'UIApplication.sharedApplication()。windows [...]''? – rintaro

2

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

+0

请看我的答案,我解决这个问题就像这样 - http://stackoverflow.com/questions/32598490/show-uiview-with-buttons-over-keyboard-like-in-skype-viber-messengers-swift-i –

0

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

我会做的是解散键盘并用相同尺寸的视图替换它的框架。

可以从列出的键盘通知here访问键盘的框架,它们的userInfo包含可用UIKeyboardFrameEndUserInfoKey访问的键。

1

雨燕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) 
0

斯威夫特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中的最后一个窗口。

0

您绝对可以将视图添加到您的应用程序的窗口,并且您还可以完全添加另一个窗口。您可以设置其框架和级别。级别可能是UIWindowLevelAlert