当键盘出现时,IOS迅速将它隐藏在前2个TextField中

时间:2015-08-26 11:37:47

标签: ios swift

当键盘出现时,这是我的整个代码,它隐藏了前1个文本字段,其中一个是半可见的。它有效但当它隐藏顶部字段时,它看起来有线。

我在网上找到了这个代码,但我无法修复它。

class ViewController2: UIViewController, ENSideMenuDelegate, UITextFieldDelegate {

@IBOutlet weak var text1: UITextField!
@IBOutlet weak var text2: UITextField!
@IBOutlet weak var text3: UITextField!
@IBOutlet weak var text4: UITextField!
@IBOutlet weak var scrollview: UIScrollView!

var activeTextField: UITextField!

@IBOutlet weak var container_view: UIView!

override func viewDidLoad() {
    super.viewDidLoad()
    //Move next line to viewWillAppear functon if you store your view controllers
    self.sideMenuController()?.sideMenu?.delegate = self
    // Do any additional setup after loading the view.

    self.text1.delegate = self
    self.text2.delegate = self
    self.text3.delegate = self
    self.text4.delegate = self
}

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

// MARK: - ENSideMenu Delegate
func sideMenuWillOpen() {
    println("sideMenuWillOpen")
}

func sideMenuWillClose() {
    println("sideMenuWillClose")
}

func sideMenuDidClose() {
    println("sideMenuDidClose")
}

func sideMenuDidOpen() {
    println("sideMenuDidOpen")
}

/*
func sideMenuShouldOpenSideMenu() -> Bool {
    println("sideMenuShouldOpenSideMenu")
    return false
}
*/

// MARK: - Keyboard

// Call this method somewhere in your view controller setup code.
func registerForKeyboardNotifications() {
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeShown:",
        name: UIKeyboardWillShowNotification,
        object: nil)
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeHidden:",
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func unregisterFromKeyboardNotifications () {
    let center:  NSNotificationCenter = NSNotificationCenter.defaultCenter()
    center.removeObserver(self, name: UIKeyboardDidShowNotification, object: nil)
    center.removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

private func stopObservingKeyboardEvents() {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}

// Called when the UIKeyboardDidShowNotification is sent.
func keyboardWillBeShown(sender: NSNotification) {

    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.CGRectValue().size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollview.contentInset = contentInsets
    scrollview.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.

    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height + 80

let activeTextFieldRect: CGRect? = activeTextField?.frame
    let activeTextFieldOrigin: CGPoint? = activeTextFieldRect?.origin
    if (!CGRectContainsPoint(aRect, activeTextFieldOrigin!)) {
        scrollview.scrollRectToVisible(activeTextFieldRect!, animated:true)
    }
}

// Called when the UIKeyboardWillHideNotification is sent
func keyboardWillBeHidden(sender: NSNotification) {
    let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
    scrollview.contentInset = contentInsets
    scrollview.scrollIndicatorInsets = contentInsets
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.registerForKeyboardNotifications()
}

override func viewDidDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

// MARK: -  Text Field

func textFieldDidBeginEditing(textField: UITextField) {
    self.activeTextField = textField
}

func textFieldDidEndEditing(textField: UITextField) {
    self.activeTextField = nil
}
}

0 个答案:

没有答案