模拟器上的奇怪行为。
View Controller有一个文本字段。选择文本字段时,会出现键盘并且视图会向上移动,以便用户可以访问屏幕底部的按钮。按文本字段外部,键盘解除并查看向下移动。很基本的东西。
最近,当我在iPhone 6s模拟器上运行应用程序时,选择文本字段会使视图向下移动,并且键盘不会出现。此外,在场外按压会使视野进一步向下移动。它曾经工作得很好。这刚刚开始,我不记得进行任何会导致问题的代码更改。
有趣的是,这只发生在运行6s模拟器时。 4s,5,5s,6,6 Plus和6S Plus都可以正常工作。此外,它在实际的6s设备上运行时工作正常。只是6s模拟器很古怪。
其他人面临类似的问题?知道如何解决这个问题吗?这是我的代码,以防万一。但如果这是一个编码问题,我会感到惊讶。谢谢!
import Foundation
import UIKit
import Parse
let defaults = NSUserDefaults.standardUserDefaults()
var verificationCode : String = String()
class VerificationEntry: UIViewController {
var keyboardInt = 0
@IBOutlet var buttonToBottom: NSLayoutConstraint!
@IBOutlet var textfieldToSubmit: NSLayoutConstraint!
@IBOutlet var titleToBottom: NSLayoutConstraint!
@IBOutlet var verify: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
defaults.setObject("Coding Explorer", forKey: "userNameKey")
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
self.deviceSize()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let verInt = defaults.valueForKey("VerInt") as! Int
if verInt < 1 {
let alert = UIAlertController(title: "Thank You", message: "A verification code will be emailed to you, pending approval.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
defaults.setInteger(1, forKey: "VerInt")
}
}
@IBAction func submitVerification(sender: AnyObject) {
verificationCode = verify.text!
let userCode = defaults.stringForKey("userCode")
var parseCode : String = String()
do {
let predicate = NSPredicate(format:"UserNumber = %@", userCode!)
let query = PFQuery(className: "OffYearUsers", predicate: predicate)
let scoreArray = try query.findObjects()
parseCode = scoreArray[0]["AccessCode"] as! String
} catch {
print("Error")
}
if verificationCode == parseCode {
defaults.setValue(parseCode, forKey: "accessCode")
performSegueWithIdentifier("verifyToMain", sender: nil)
defaults.setInteger(0, forKey: "ThankYou")
}
}
func keyboardWillShow(sender: NSNotification) {
if keyboardInt < 1 {
self.view.frame.origin.y -= 140
keyboardInt++
}
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 140
keyboardInt = 0
}
func dismissKeyboard() {
view.endEditing(true)
}
func deviceSize() {
if UIDevice().userInterfaceIdiom == .Phone {
switch UIScreen.mainScreen().nativeBounds.height {
case 960:
buttonToBottom.constant = 40
textfieldToSubmit.constant = 75
titleToBottom.constant = 340
case 1136:
buttonToBottom.constant = 100
textfieldToSubmit.constant = 90
titleToBottom.constant = 425
default:
print("unknown")
}
}
}
}
答案 0 :(得分:1)
首先重置模拟器并切换键盘 当没有显示键盘时,硬件 - 键盘 - 切换软件键盘适用于我
答案 1 :(得分:0)
当硬件键盘连接到设备时,您看到的行为是正确的行为(具有BT键盘的真实设备和启用菜单项时的SIM卡)。您在6s而不是其他设备中看到此行为的原因很可能是因为您在该设备中的设置与其他设备不同。点击cmd-k切换屏幕键盘(类似于双击连接到真实设备的蓝牙键盘上的弹出按钮)。