我正在使用创建一个应用程序UINavigationController
呈现下一个视图控制器。
iOS5 提供了一种新的呈现方法UIViewControllers
:
presentViewController:animated:completion:
现在我问我为什么没有一个完成处理程序UINavigationController
?
仅有
pushViewController:animated:
是否可以像新的那样创建我自己的完成处理程序presentViewController:animated:completion:
?
See 帕的回答 https://stackoverflow.com/a/33767837/246776另一个更新的解决方案
UINavigationController
动画运行于CoreAnimation
,因此将代码封装在其中是有意义的CATransaction
从而设置一个完成块。
Swift:
为了快速,我建议创建一个这样的扩展
extension UINavigationController {
public func pushViewController(viewController: UIViewController,
animated: Bool,
completion: @escaping (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
pushViewController(viewController, animated: animated)
CATransaction.commit()
}
}
Usage:
navigationController?.pushViewController(vc, animated: true) {
// Animation done
}
Objective-C
Header:
#import <UIKit/UIKit.h>
@interface UINavigationController (CompletionHandler)
- (void)completionhandler_pushViewController:(UIViewController *)viewController
animated:(BOOL)animated
completion:(void (^)(void))completion;
@end
执行:
#import "UINavigationController+CompletionHandler.h"
#import <QuartzCore/QuartzCore.h>
@implementation UINavigationController (CompletionHandler)
- (void)completionhandler_pushViewController:(UIViewController *)viewController
animated:(BOOL)animated
completion:(void (^)(void))completion
{
[CATransaction begin];
[CATransaction setCompletionBlock:completion];
[self pushViewController:viewController animated:animated];
[CATransaction commit];
}
@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)