【问题标题】:Move the screen up when keyboard appears depending of the textView. Swift当键盘出现时根据 textView 向上移动屏幕。迅速
【发布时间】:2016-04-13 19:30:19
【问题描述】:

我尝试在键盘出现时向上移动屏幕。我成功地做到了,但我需要知道如何仅在单击底部UITextView 时滚动视图,而不是顶部TextView,因为键盘只触及下面的TextView

应用是这样的:

Our view

class viewControllerCuatro: UIViewController, UITextViewDelegate {

 @IBOutlet weak var textViewInteriorUno: UITextView!
@IBOutlet weak var textViewInteriorDos: UITextView!

override func viewDidLoad() {


self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Atrás", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)


**NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: self.view.window)**




}


 **func keyboardWillShow(sender: NSNotification) {**



// 1
let userInfo: [NSObject : AnyObject] = sender.userInfo!
// 2
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
let offset: CGSize = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue.size
// 3
if keyboardSize.height == offset.height {
    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.view.frame.origin.y -= keyboardSize.height
    })
} else {
    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.view.frame.origin.y += keyboardSize.height - offset.height
    })
    }


}

**func keyboardWillHide(sender: NSNotification)** {
let userInfo: [NSObject : AnyObject] = sender.userInfo!
let keyboardSize: CGSize = userInfo[UIKeyboardFrameBeginUserInfoKey]!.CGRectValue.size
self.view.frame.origin.y += keyboardSize.height
}

**override func viewWillDisappear(animated: Bool)** {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: self.view.window)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: self.view.window)
}


  override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

if textViewInteriorUno.text == ""  || textViewInteriorUno.text.isEmpty == true || textViewInteriorDos.text == "" || textViewInteriorDos.text.isEmpty == true {


    textViewInteriorUno.resignFirstResponder()


    textViewInteriorDos.resignFirstResponder()
}
else{

    self.textViewInteriorUno.resignFirstResponder()

    self.textViewInteriorDos.resignFirstResponder()
}
}

   func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool{

var shouldReplace = true // For most cases, return true, only modify if we have a special case

// If the textView is 'textViewInteriorUno'
if textView.isEqual(textViewInteriorUno) {

let newLength = textViewInteriorUno.text.utf16.count + text.utf16.count - range.length
shouldReplace = newLength <= 12           // will be true if the length is <= 12

}

else if textView.isEqual(textViewInteriorDos) {

let newLength = textViewInteriorDos.text.utf16.count + text.utf16.count - range.length
shouldReplace = newLength < 13 // Will be true if the length > 6


}

return shouldReplace
}

【问题讨论】:

标签: ios iphone swift


【解决方案1】:

IQKeyboardManager 对我来说是天赐之物!

第 1 步:

您所要做的就是从 https://github.com/hackiftekhar/IQKeyboardManager 下载项目并将 IQKeyboardManagerSwift 目录从演示项目移动到您的项目。

确保 IQKeyboardManagerSwift 目录在您移动后位于项目中的正确位置。

第 2 步:

然后在 AppDelegate.swift 中,在你的应用函数中启用 IQKeyboardManager。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
  {

  // This is the only line of code you need to manually add
  // in order for IQKeybaordManager to work. Thats it.
  IQKeyboardManager.sharedManager().enable = true

  return true
  }
}

就是这样!现在您的键盘将向上滑动并相应地移动视图。给这个框架的创建者的道具,让我们的生活变得更轻松。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多