MetovaBase 2.0.0

MetovaBase 2.0.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布最后发布2018年9月
SPM支持SPM

Logan GauthierNick GriffithChris DillardChris MartinBeverly MassengillMetova, Inc.维护。



  • Nick Griffith和Logan Gauthier

MetovaBase

Build Status CocoaPods Compatible Documentation Coverage Status Platform Twitter

要求

  • iOS 8.0
  • Swift 4.2

安装

MetovaBase可以通过CocoaPods获取。要安装它,只需在Podfile中添加以下行:

pod 'MetovaBase'

概述

MetovaBase提供了实现常用工具的实用UIKit子类。

BaseViewController

BaseViewController 类继承自 UIViewController 并添加了一些我们在大多数(如果不是所有)的视图控制器类中都会添加的通用功能。在 viewDidLoad 方法中进行了额外的设置来为您提供的视图控制器提供键盘通知。如果对其进行重写,请确保在您的子类中调用 super.viewDidLoad

使用示例

import MetovaBase

class MyViewController: BaseViewController {

    override func viewDidLoad() {
    
    	// this call registers your VC for keyboard notifications
    	super.viewDidLoad() 
    	
    	// my custom setup here
    }
}

此外,BaseViewController 在其 deinit() 方法中将您的视图控制器注销,使其不会接收到键盘通知。

键盘通知代理

此协议包含一系列方法,BaseViewController 的子类(或扩展)可以实现这些方法来接收键盘通知。如前所述,您不需要在子类或扩展中注册或注销这些通知,只需实现这些方法即可。

方法

这些方法将在启动相应的键盘通知时被调用。

@objc optional func keyboardWillShow(notification: Notification)

@objc optional func keyboardDidShow(notification: Notification)

@objc optional func keyboardWillChangeFrame(notification: Notification)

@objc optional func keyboardDidChangeFrame(notification: Notification)

@objc optional func keyboardWillHide(notification: Notification)

@objc optional func keyboardDidHide(notification: Notification)

BaseViewControllerConvenience

BaseViewControllerConvenience 提供了一个方便的方法来关闭键盘。它假定第一响应者是您的视图控制器视图的后代。由于它是 BaseViewController 的扩展,您可以在您的任何 BaseViewController 子类中调用该方法,如下所示

dismissKeyboard()

或者作为选择器如下所示

dismissGestureRecognizer.addTarget(self, action: #selector(dismissKeyboard))

BaseViewControllerFrames

BaseViewControllerFrames为您的视图控制器提供了处理键盘的一些额外辅助方法。同样,这扩展了BaseViewController,因此这些方法在您的BaseViewController子类中的任何地方都是可用的。

方法

public func framesForKeyboard(notification: Notification) -> (fromFrame: CGRect, toFrame: CGRect) 

此方法返回键盘的起始和结束frame。它会从传入的notification中解析frame信息。如果在notification对象中没有frame信息,则返回CGRect.zero

public func adjustContentInset(scrollview scrollView: UIScrollView, forKeyboardWillChangeFrameNotification notification: Notification)

如名称所示,此方法根据键盘frame调整scrollview的内边距。对于键盘可能覆盖第一响应者的视图,这很有帮助。

示例用法

import MetovaBase

class MyViewController: BaseViewController, KeyboardNotificationDelegate {

	@IBOutlet myScrollView: UIScrollView!

	@objc func keyboardDidChangeFrame(notification: Notification) {
	
		let frames = framesForKeyboard(notification: notification)
		let fromFrame = frames.fromFrame
		let toFrame = frames.toFrame
		
		// do whatever you need to with the 'fromFrame' and 'toFrame'
	}

	@objc func keyboardWillChangeFrame(notification: Notification) {

		adjustContentInset(scrollView: myScrollView, forKeyboardWillChangeNotification: notification)
	}
}

致谢

MetovaBase由Metova Inc.拥有和维护。

贡献者

如果您想为Metova Test Kit做出贡献,请查看我们的CONTRIBUTING指南。

Metova Test Kit横幅图像和其他资产由Christi Johnson提供。

许可

MetovaBase在MIT许可下提供。有关更多信息,请参阅LICENSE文件。