react-native-navigation的迁移库

RNNBackButtonOptions.m 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #import "RNNBackButtonOptions.h"
  2. #import "UIImage+tint.h"
  3. @implementation RNNBackButtonOptions
  4. - (void)applyOn:(UIViewController *)viewController {
  5. if (self.icon) {
  6. UIImage *image = self.tintedIcon;
  7. [viewController.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
  8. [viewController.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
  9. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:nil action:nil];
  10. [self setBackItem:backItem onViewController:viewController];
  11. } else if (self.title) {
  12. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:self.title
  13. style:UIBarButtonItemStylePlain
  14. target:nil
  15. action:nil];
  16. [self setBackItem:backItem onViewController:viewController];
  17. }
  18. if (self.visible) {
  19. viewController.navigationItem.hidesBackButton = ![self.visible boolValue];
  20. }
  21. if (self.showTitle && ![self.showTitle boolValue]) {
  22. self.title = @"";
  23. }
  24. }
  25. - (void)setBackItem:(UIBarButtonItem *)backItem onViewController:(UIViewController *)viewController {
  26. UINavigationController* nvc = viewController.navigationController;
  27. if (nvc.viewControllers.count >= 2) {
  28. UIViewController* lastViewControllerInStack = nvc.viewControllers[nvc.viewControllers.count - 2];
  29. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  30. }
  31. }
  32. - (UIImage *)tintedIcon {
  33. UIImage *image = self.icon ? [RCTConvert UIImage:self.icon] : nil;
  34. if (self.color) {
  35. return [[image withTintColor:[RCTConvert UIColor:self.color]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  36. }
  37. return image;
  38. }
  39. @end