我们有一个看起来像这样的应用程序:
我们在视图的顶部和底部有UITextViews
,我一直在寻求帮助,以便在显示keyboard
时移动屏幕。
它确实很完美,但我想知道如何仅将此应用于屏幕底部的UITextView
(textViewInteriorDos
),之前触摸键盘。
如何在这个方法中正确应用if/ else
语句?
键盘没有顶部UITextView
,因此我们不需要它。
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
}
答案 0 :(得分:0)
我认为你应该在你的控制器上跟踪textView isActive,当keyboardWillShow:
运行时,如果你想要激活textView,你将改变约束,如果不是你返回。您可以检查它是isFirstResponder()
,还是可以使用UITextViewDelegate
两个功能:
textViewShouldBeginEditing
和textViewShouldEndEditing
。
希望这有帮助!
答案 1 :(得分:0)
我认为它可以帮到你,如下所示
制备
@IBOutlet weak var textSkillSearch: UITextView!
@IBOutlet weak var textWillSearch: UITextView!
覆盖func viewDidLoad(){ super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MainViewController.textViewDidBeginEditing(_:)), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(MainViewController.keyboardWillHide(_:)), name:UIKeyboardWillHideNotification, object: nil);
self.textWillSearch.delegate = self
self.textSkillSearch.delegate = self
}
////
func textViewDidBeginEditing(textView: UITextView) {
if textView == textWillSearch {
self.view.frame.origin.y = -130
}
}
func textViewDidEndEditing(textView: UITextView) {
self.view.frame.origin.y = 0
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y = 0
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
// called when 'return' key pressed. return NO to ignore.
func textViewShouldEndEditing(textView: UITextView) -> Bool {
textView.resignFirstResponder()
return true
}
// called when users tap out of textfield
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
答案 2 :(得分:0)
您可以覆盖方法keyboardWillShow(notification: NSNotification)
,并在其中检查谁是firstResponder,如下所示:
if descriptionTextView.isFirstResponder {
// do your staff here
}
答案 3 :(得分:0)
在通知中调用此功能。
@objc func keyboardWillAppear(notification: NSNotification)
{
let keyFrame = notification.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
let keyY:CGFloat = keyFrame.origin.y
textView.frame.origin.y = keyY - textView.frame.height
}
@objc func hideKeyboard()
{
textView.frame.origin.y = whatever you want
}