而不是虚拟键盘黑框出现并转移应用程序

时间:2016-01-31 03:40:27

标签: ios xcode swift

我是Xcode / Swift的初学者。我正在使用Xcode 7.2。

我构建了一个应用程序,它具有在单击文本框时调出键盘的功能,并在单击其他位置时移除键盘。

当我在模拟器中运行我的项目时,屏幕顶部会出现一个黑盒子而不是虚拟键盘,而是将应用程序向下移动。单击任何其他文本框会导致屏幕顶部的黑框增长。

App after a text field is selected

我尝试在模拟器中通过iPhone6Plus运行iPhone5,并且所有这些都发生了。使用iPhone4s时,虚拟键盘会按预期弹出,但所有视图都会失真。

知道为什么会这样吗?

以下是视图控制器中的代码,如果它有帮助:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {


    @IBOutlet weak var runningTimeTextField: UITextField!
    @IBOutlet weak var strengthTimeTextField: UITextField!
    @IBOutlet weak var runningCalTextField: UITextField!
    @IBOutlet weak var strengthCalTextField: UITextField!
    @IBOutlet weak var foodIntakeTextField: UITextField!
    @IBOutlet weak var weightTextField: UITextField!
    @IBOutlet weak var bloodPressureTextField: UITextField!
    @IBOutlet weak var submitButton: UIButton!
    @IBOutlet weak var intakeLabel: UILabel!
    @IBOutlet weak var caloriesBurnedLabel: UILabel!
    @IBOutlet weak var pressureLabel: UILabel!
    @IBOutlet weak var weightChangeLabel: UILabel!
    @IBOutlet weak var weightChangeLabel2: UILabel!

    var count : Int = 0

    var calc : calcModel = calcModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Initialize text boxes to 0
        runningTimeTextField.text = "0"
        strengthTimeTextField.text = "0"
        runningCalTextField.text = "0"
        strengthCalTextField.text = "0"
        foodIntakeTextField.text = "0"
        weightTextField.text = "0"
        bloodPressureTextField.text = "0"

        //Set report lablels to hidden
        self.intakeLabel.hidden=true
        self.caloriesBurnedLabel.hidden=true
        self.pressureLabel.hidden=true
        self.weightChangeLabel.hidden=true
        self.weightChangeLabel2.hidden=true


        //Added for keyboard stuff:
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);


        runningTimeTextField.delegate=self
        strengthTimeTextField.delegate=self
        runningCalTextField.delegate=self
        strengthCalTextField.delegate=self
        foodIntakeTextField.delegate=self
        weightTextField.delegate=self
        bloodPressureTextField.delegate=self

    }

    //Added for keyboard stuff:
    func keyboardWillShow(sender: NSNotification) {
        self.view.frame.origin.y -= 100
    }
    func keyboardWillHide(sender: NSNotification) {
        self.view.frame.origin.y += 100
    }

/*
    //Displays error that function is not overwritten.
    //Make the keyboard disapear as user touches outside the  text boxes
    override func touchesBegan(touches: Set<NSObject>,withEvent event: UIEvent) {
        view.endEditing(true)
        self.runningTimeTextField.resignFirstResponder()
        self.strengthTimeTextField.resignFirstResponder()
        self.runningCalTextField.resignFirstResponder()
        self.strengthCalTextField.resignFirstResponder()
        self.foodIntakeTextField.resignFirstResponder()
        self.weightTextField.resignFirstResponder()
        self.bloodPressureTextField.resignFirstResponder()
    }
*/
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
        self.view.endEditing(true)
    }


    func textFieldShouldReturn(textField: UITextField) -> Bool {
        self.runningTimeTextField.resignFirstResponder()
        self.strengthTimeTextField.resignFirstResponder()
        self.runningCalTextField.resignFirstResponder()
        self.strengthCalTextField.resignFirstResponder()
        self.foodIntakeTextField.resignFirstResponder()
        self.weightTextField.resignFirstResponder()
        self.bloodPressureTextField.resignFirstResponder()
        return true;
    }


    @IBAction func submitPressed(sender: UIButton) {
        //Reveal output labels
        self.intakeLabel.hidden=false
        self.caloriesBurnedLabel.hidden=false
        self.pressureLabel.hidden=false
        self.weightChangeLabel.hidden=false
        self.weightChangeLabel2.hidden=false

        //Get input values
        let calBurnedRunning = Int(runningCalTextField.text!)
        let calBurnedStrength = Int(strengthCalTextField.text!)
        let intakeNum = Int(foodIntakeTextField.text!)
        let weight = Double(weightTextField.text!)
        let pressure = Double(bloodPressureTextField.text!)

        count++

        //Use calc model to calculate health report
        intakeLabel.text = String(calc.calculateCalories(intakeNum!, count: count))

        caloriesBurnedLabel.text = String(calc.calculateCaloriesBurned(calBurnedRunning!, calBurnedStrength: calBurnedStrength!, count: count))

        pressureLabel.text = String(format: "%.2f", calc.calculateBloodPressure(pressure!, count: count))

        weightChangeLabel.text = String(format: "%.2f", calc.weightChange(weight!, count: count))

         weightChangeLabel2.text = String(format: "%.2f", calc.weightChangeWeek(count))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

1 个答案:

答案 0 :(得分:0)

有两个问题。一个是这段代码:

func keyboardWillShow(sender: NSNotification) {
    self.view.frame.origin.y -= 100
}
func keyboardWillHide(sender: NSNotification) {
    self.view.frame.origin.y += 100
}

您不应该弄乱视图控制器视图的框架。

另一个是这一行:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?){
    self.view.endEditing(true)
}

这导致键盘在有机会出现之前被解雇。