导航栏后退按钮文本没有改变

bir*_*age 2 xcode objective-c uinavigationbar ios

我正在尝试更改导航栏后退按钮文本的文本。我已经尝试了此处提供的解决方案,但无法在后退按钮上获得所需的文本。在 appdelegate 中,我设置了这些功能:

 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];
 [[UINavigationBar appearance] setBarTintColor:[self colorWithHexString:@"e74c3c"]];   

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor whiteColor], UITextAttributeTextColor,
  [UIFont fontWithName:@"Futura-CondensedMedium" size:19.0], UITextAttributeFont,nil]];

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowOffset = CGSizeMake(0.0, 1.0);
shadow.shadowColor = [UIColor whiteColor];

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil]
 setTitleTextAttributes:
 @{NSForegroundColorAttributeName:[UIColor whiteColor],
   NSShadowAttributeName:shadow,
   NSFontAttributeName:[UIFont fontWithName:@"Futura-CondensedMedium" size:19.0]
   }
 forState:UIControlStateNormal];

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

viewdidload我设置的每个视图控制器中:

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"SomeText" style:UIBarButtonItemStylePlain target:nil action:nil];
Run Code Online (Sandbox Code Playgroud)

或者

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 5

如果我没有误解你可以这样做来设置后退按钮的标题。假设有三个视图控制器:A,BC。现在您希望后退按钮的标题为“MyViewA” AB然后您需要将此代码放在您的 ViewController 的“viewWillAppear:”中A(这将显示后退按钮的标题为“MyViewA”,当您在视图控制器上B)。

 //viewWillAppear of ViewController A
 -(void)viewWillAppear:(BOOL)animated{

       //now set title of back button
       self.navigationItem.backBarButtonItem =
       [[UIBarButtonItem alloc] initWithTitle:@"YourTitle"
                                  style:UIBarButtonItemStyleBordered
                                 target:nil
                                 action:nil];
 }
Run Code Online (Sandbox Code Playgroud)

同样,如果您从Bto导航C并希望为 ViewController 设置后退按钮标题C,则需要将上述代码放在 ViewController 的“viewWillAppear:”委托中B。简而言之,您需要在当前视图控制器上设置下一个视图控制器的标题。