Browse Source

Fix backButton properties on iOS (#3999)

* Fix ability to add color for back button

* Change docs because color can be used with iOS too

* Simplify code more

* Move icon logic to one function

* Make returning more clear

* Remove title setting because it is in setBackItem

* Make setBackItem function more clear

* Looks like the if is not needed

* Update RNNBackButtonOptions.m

* Update RNNBackButtonOptions.m
Henrik Raitasola 6 years ago
parent
commit
2cd264ec83
2 changed files with 28 additions and 30 deletions
  1. 27
    28
      lib/ios/RNNBackButtonOptions.m
  2. 1
    2
      lib/src/interfaces/Options.ts

+ 27
- 28
lib/ios/RNNBackButtonOptions.m View File

@@ -12,42 +12,41 @@
12 12
 }
13 13
 
14 14
 - (void)applyOnNavigationController:(UINavigationController *)navigationController {
15
-	if (self.showTitle && ![self.showTitle boolValue]) {
16
-		self.title = @"";
17
-	}
18
-	
19
-	if (self.icon) {
20
-		UIImage *image = self.tintedIcon;
21
-		[navigationController.navigationBar setBackIndicatorImage:[UIImage new]];
15
+  UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
16
+  	UIImage* tintedIcon = self.tintedIconIfAvailable;
17
+	if (tintedIcon) {
18
+		backItem.image = tintedIcon;
19
+		[navigationController.navigationBar setBackIndicatorImage:[UIImage new]];	
22 20
 		[navigationController.navigationBar setBackIndicatorTransitionMaskImage:[UIImage new]];
23
-		
24
-		UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:nil action:nil];
25
-		[self setBackItem:backItem onNavigationController:navigationController];
26
-	} else if (self.title) {
27
-		UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:self.title
28
-																	 style:UIBarButtonItemStylePlain
29
-																	target:nil
30
-																	action:nil];
31
-		
32
-		[self setBackItem:backItem onNavigationController:navigationController];
33
-		
34 21
 	}
22
+  
23
+	if (self.color) {
24
+	  	backItem.tintColor = [RCTConvert UIColor:self.color];
25
+	}
26
+  
27
+	if (self.showTitle && ![self.showTitle boolValue]) {
28
+	  	self.title = @"";
29
+	}
30
+  
31
+  	[self setBackItem:backItem onNavigationController:navigationController];
35 32
 }
36 33
 
37 34
 - (void)setBackItem:(UIBarButtonItem *)backItem onNavigationController:(UINavigationController *)navigationController {
38
-	if (navigationController.viewControllers.count >= 2) {
39
-		UIViewController* lastViewControllerInStack = navigationController.viewControllers[navigationController.viewControllers.count - 2];
40
-		lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
41
-	}
35
+	NSArray *viewControllers = navigationController.viewControllers;
36
+	UIViewController *lastViewControllerInStack = [viewControllers lastObject];
37
+	backItem.title = self.title ? self.title : lastViewControllerInStack.navigationItem.title;
38
+	lastViewControllerInStack.navigationItem.backBarButtonItem = backItem;
42 39
 }
43 40
 
44
-- (UIImage *)tintedIcon {
45
-	UIImage *image = self.icon ? [RCTConvert UIImage:self.icon] : nil;
46
-	if (self.color) {
47
-		return [[image withTintColor:[RCTConvert UIColor:self.color]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
41
+- (UIImage *)tintedIconIfAvailable {
42
+	if (self.icon) {
43
+		UIImage *image = [RCTConvert UIImage:self.icon];
44
+	  	return self.color
45
+	  		? [[image withTintColor:[RCTConvert UIColor:self.color]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
46
+	  		: image;
48 47
 	}
49
-	
50
-	return image;
48
+  
49
+  	return nil;
51 50
 }
52 51
 
53 52
 @end

+ 1
- 2
lib/src/interfaces/Options.ts View File

@@ -173,8 +173,7 @@ export interface OptionsTopBarBackButton {
173 173
    */
174 174
   showTitle?: boolean;
175 175
   /**
176
-   * Back button icon or text color
177
-   * #### (Android specific)
176
+   * Back button icon and text color
178 177
    */
179 178
   color?: Color;
180 179
 }