react-native-navigation的迁移库

RNNBackButtonOptions.m 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #import "RNNBackButtonOptions.h"
  2. #import "UIImage+tint.h"
  3. @implementation RNNBackButtonOptions
  4. - (void)applyOn:(UIViewController *)viewController {
  5. if (self.showTitle && ![self.showTitle boolValue]) {
  6. self.title = @"";
  7. }
  8. if (self.icon) {
  9. UIImage *image = self.tintedIcon;
  10. [viewController.navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
  11. [viewController.navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
  12. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:nil action:nil];
  13. [self setBackItem:backItem onViewController:viewController];
  14. } else {
  15. NSString *title;
  16. title = self.title ? self.title : viewController.navigationItem.title;
  17. UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:title
  18. style:UIBarButtonItemStylePlain
  19. target:nil
  20. action:nil];
  21. if (self.color) {
  22. [backItem setTintColor:[RCTConvert UIColor:self.color]];
  23. }
  24. if(self.fontFamily || self.fontSize) {
  25. NSNumber* fontSize = self.fontSize;
  26. NSString* fontFamily = self.fontFamily;
  27. NSMutableDictionary* textAttributes = [[NSMutableDictionary alloc] init];
  28. UIFont *font;
  29. if (!fontSize) {
  30. fontSize = [[NSNumber alloc] initWithInt: 18];
  31. }
  32. font = fontFamily ?
  33. [UIFont fontWithName:fontFamily size:[fontSize floatValue]] :
  34. [UIFont systemFontOfSize:[fontSize floatValue]];
  35. [textAttributes setObject:font forKey:NSFontAttributeName];
  36. [backItem setTitleTextAttributes:textAttributes forState:UIControlStateNormal];
  37. [backItem setTitleTextAttributes:textAttributes forState:UIControlStateHighlighted];
  38. }
  39. [self setBackItem:backItem onViewController:viewController];
  40. }
  41. if (self.visible) {
  42. viewController.navigationItem.hidesBackButton = ![self.visible boolValue];
  43. }
  44. }
  45. - (void)setBackItem:(UIBarButtonItem *)backItem onViewController:(UIViewController *)viewController {
  46. UINavigationController* nvc = viewController.navigationController;
  47. if (nvc.viewControllers.count >= 2) {
  48. UIViewController* lastViewControllerInStack = nvc.viewControllers[nvc.viewControllers.count - 2];
  49. lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
  50. }
  51. }
  52. - (UIImage *)tintedIcon {
  53. UIImage *image = self.icon ? [RCTConvert UIImage:self.icon] : nil;
  54. if (self.color) {
  55. return [[image withTintColor:[RCTConvert UIColor:self.color]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
  56. }
  57. return image;
  58. }
  59. @end