我需要检查iOS应用中键盘可见性的状况.
伪代码:
if(keyboardIsPresentOnWindow) {
//Do action 1
}
else if (keyboardIsNotPresentOnWindow) {
//Do action 2
}
Run Code Online (Sandbox Code Playgroud)
我该如何检查这种情况?
我在这里看到的帖子表明,UIScrollViews如果子视图UITextField成为第一个响应者,应自动滚动; 但是,我无法弄清楚如何让它发挥作用.
我已经是一个UIViewController具有UIScrollView与内UIScrollView有多个文本框.
如果有必要,我知道如何手动完成此操作; 但是,从我一直在阅读的内容来看,似乎可以让它自动滚动.请帮忙.
我刚开始在XCode 7中进行UI测试并遇到了这个问题:
我需要在文本字段中输入文本然后单击按钮.不幸的是,这个按钮隐藏在键盘后面,在将文本输入文本字段时出现.XCode正在尝试滚动以使其可见但我的视图不可滚动因此失败.
我目前的解决方案是:
let textField = app.textFields["placeholder"]
textField.tap()
textField.typeText("my text")
app.childrenMatchingType(.Window).elementBoundByIndex(0).tap() // hide keyboard
app.buttons["hidden button"].tap()
Run Code Online (Sandbox Code Playgroud)
我可以这样做,因为我的ViewController正在拦截触摸:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
view.endEditing(false)
super.touchesBegan(touches, withEvent: event)
}
Run Code Online (Sandbox Code Playgroud)
我对我的解决方案并不满意,在UI测试期间有没有其他方法可以隐藏键盘?
如何在iOS swift中检测键盘高度变化或键盘更改?
看下面的代码,它在文本键盘和Emojis键盘中显示非常小的一行:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardHideNShow(_:)), name: UIKeyboardWillHideNotification, object: nil)
var didDisplayedKeyboard:Bool = false
func keyboardHideNShow(notification:NSNotification) {
var movement: CGFloat = 0.0
let userInfo:NSDictionary = notification.userInfo!
let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.CGRectValue()
let movementDuration:NSTimeInterval = 0.3
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
if notification.name == UIKeyboardWillShowNotification {
// Do the operation only if its hide n show or show n hide
// When the keyboard switches from text …Run Code Online (Sandbox Code Playgroud) ios ×3
keyboard ×2
objective-c ×2
swift ×2
iphone ×1
testing ×1
uiscrollview ×1
uitextfield ×1