2015-09-04 5 views
7

inputAccessoryViewで「Attach」ボタンをタップすると、キーボードで簡単なビューを作成したいと考えています。このような 何か: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

あなたは 'UIApplication.sharedApplication()。windows [...]'の代わりに 'sender.window'を使うことができますか? – 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

これは最前面のウィンドウにアクセスすることで可能ですが、は、アップルのガイドラインを明らかに妨げているので、を避けてください。

私がやることは、キーボードを外してフレームを同じ寸法のビューに置き換えることです。

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) 

トリックは、キーボードを保持しているUIWindowにトップサブビューとしてcustomViewを追加することです - そしてそれが起こります最後のウィンドウはUIApplication.shared.windowsです。

0

ビューをアプリケーションのウィンドウに追加することはできますが、別のウィンドウを完全に追加することもできます。フレームとレベルを設定できます。レベルはUIWindowLevelAlertです。

関連する問題