我目前正在开发一个带有登录和注册表单的 iOS 应用程序。为了确保键盘不覆盖任何UITextField
s,我实施了Apple提供的以下解决方案,并在本期中进行了讨论。
简单总结一下,此解决方案使用 a UIScrollView
,其中放置了不同的 UI 元素,UIKeyboardDidShowNotification
并UIKeyboardDidHideNotification
在键盘出现/消失时上下移动元素,以便UITextField
不隐藏 s。
除了一件事之外,这就像一个魅力:对于我所有的UIViewController
s,我必须重复相同的代码。为了解决我的问题,我尝试过:
- 创建一个 base
UIViewController
,为不同的功能提供实现,可以是其他UIViewController
的子类; - 使用协议和协议扩展为不同的功能提供默认实现,并使 my
UIViewController
s 符合它。
两种解决方案都没有解决我的问题。UIScrollView
对于第一个解决方案,尽管已声明,但我无法通过 Interface Builder 连接我的基类。
@IBOutlet weak var scrollView: UIScrollView!
在尝试实现第二个解决方案时,UIViewController
实现我的协议以某种方式无法识别声明的方法及其实现。
协议声明:
protocol ScrollViewProtocol {
var scrollView: UIScrollView! { get set }
var activeTextField: UITextField? { get set }
func addTapGestureRecognizer()
func singleTapGestureCaptured()
func registerForKeyboardNotifications()
func deregisterForKeyboardNotifications()
func keyboardWasShown(notification: NSNotification)
func keyboardWillBeHidden(notification: NSNotification)
func setActiveTextField(textField: UITextField)
func unsetActiveTextField()
}
协议扩展实现了addTapGestureRecognizer()
我希望避免使用的所有功能@objc
:
extension ScrollViewProtocol where Self: UIViewController {
// The implementation for the different functions
// as described in the provided links expect for the following method
func registerFromKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidShowNotification, object: nil, queue: nil, usingBlock: { notification in
self.keyboardWasShown(notification)
})
NSNotificationCenter.defaultCenter().addObserverForName(UIKeyboardDidHideNotification, object: nil, queue: nil, usingBlock: { notification in
self.keyboardWillBeHidden(notification)
})
}
}
有没有人对我的问题有很好的解决方案,我知道如何避免UITextField
在键盘出现/消失时重复与上下移动 s 相关的代码?或者有谁知道为什么我的解决方案不起作用?