第一步:添加新的UIViewController,命名为MainPageViewController。
步骤 2: 在 MainInterface Storyboard 中添加新的 View Controller,在 Storyboard 的 Custom Class 部分将其类更改为 MainPageViewController,将 Storyboard ID 设置为 @987654327 @ 在身份部分。
第 3 步: 打开 Share 扩展的 info.plist
默认情况下它看起来像这样-
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>TRUEPREDICATE</string>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
现在代替默认的NSExtensionMainStoryboard 键使用NSExtensionPrincipalClass 键,所以最终结果将是
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<string>TRUEPREDICATE</string>
</dict>
<key>NSExtensionPrincipalClass</key>
<string>HomeViewController</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
HomeViewController 是我们的新入口点控制器。
第四步:现在,最重要的我们需要fix weird issue related with module naming。
要修复它,我们需要在 HomeViewController 文件的顶部添加 @objc(HomeViewController)。
Step5:同样为了动画演示,我们需要在viewWillAppear中添加动画代码
参见下面的参考代码:
import UIKit
@objc(HomeViewController)
class HomeViewController : UINavigationController {
init() {
let viewController:UIViewController = UIStoryboard(name: "MainInterface", bundle: nil).instantiateViewController(withIdentifier: "MainPageViewController") as UIViewController
super.init(rootViewController: viewController)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.view.transform = CGAffineTransform(translationX: 0, y: self.view.frame.size.height)
UIView.animate(withDuration: 0.25, animations: { () -> Void in
self.view.transform = CGAffineTransform.identity
})
}
}
其中MainPageViewController 是您的MainViewController 的标识符,我们要展示它。
第六步:我们可以在MainPageViewController类中创建新函数:
func hideExtensionWithCompletionHandler(completion:@escaping (Bool) -> Void) {
UIView.animate(withDuration: 0.20, animations: {
self.navigationController!.view.transform = CGAffineTransform(translationX: 0, y: self.navigationController!.view.frame.size.height)
}, completion: completion)
}
在保存或取消按钮和块内调用上面的func,我们可以调用completeRequest或cancelRequest(withError:)
func saveButtonTapped(sender: UIBarButtonItem) {
self.hideExtensionWithCompletionHandler(completion: { (Bool) -> Void in
self.extensionContext!.completeRequest(returningItems: nil, completionHandler: nil)
})
}
现在,做任何你想做的人,做双重人;-)