0

I have a problem about UIBarButtonItem on NavigationBar.

  1. Transit from FirstViewController to SeconderViewController by pusuViewController.
  2. Return to FirstViewController with return button on left side of NavigationBar.
  3. [Problem happen] The Color of [next] button on right side of NavigationBar is transparent.

(You can tap [next] button althought the color is transparent )

This problem happen on iPhone8(iOS11.2.1(15C153), not heppen on iPhone6(iOS10.3.3(14G60)).

enter image description here

My code is below,

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
    self.window.backgroundColor = UIColor.blackColor;

    UIViewController *vc = [[FirstViewController alloc] init];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc];
    self.window.rootViewController = nc;
    [self.window makeKeyAndVisible];

    return YES;
}

FirstViewContrtoller.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title = @"First View";
    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"next", nil)
                                                      style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:@selector(touchUpNextButton:)];
    self.navigationItem.rightBarButtonItem = nextButton;
}

- (void)touchUpNextButton:(id)sender
{
    UIViewController *vc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

SecondViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.title = @"Second View";
}

I'd appreciate if you would provide me a good solution.

Thank you.

M.Masa
  • 532
  • 4
  • 20
  • Code looks good, but the button maybe bug itself staying in pressed state instead of `.normal` – mcgtrt Dec 20 '17 at 08:10
  • Sorry I could not understand your comment. Could you write the code of your solution ? – M.Masa Dec 20 '17 at 08:23
  • are you using pop method while return to first view or using push method please mention this as well – Akash Raghani Dec 20 '17 at 11:06
  • @AkashRaghani I don't write the method to return to First View. iOS automatically put the "< FIrst View" button on the left side of navigation bar of Second View. – M.Masa Dec 21 '17 at 01:29

1 Answers1

1

I think it is the bugs of iOS 11.2.1 You can temporary fix by following solution:

UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"next", nil)
                                                      style:UIBarButtonItemStylePlain
                                                     target:self
                                                     action:@selector(touchUpNextButton:)];
[nextButton setTitleTextAttributes:@{NSForegroundColorAttributeName : [self.view tintColor], NSFontAttributeName:[UIFont systemFontOfSize:16.9f]} forState:UIControlStateNormal];

Hope that can help you.

  • Thank you for your code. It seems to work correctly. By the way, do you know the way to get system default font size of BarButtonItem text ? You assigned 16.9f point in your code but how to know the size ? And I wonder if the size is depending on the display size of iPhone. So I'd like to know the way to get the font size before set the `TitleTextAttribute` – M.Masa Dec 21 '17 at 02:31
  • @M.Masa you can `Debug View Hierarchy ` by following this answer: https://stackoverflow.com/a/26052806/3326412 – motbantuangiauten Dec 21 '17 at 02:35
  • You can know the size with `Debug View Hierarchy`. But I'd like to know how to get the size in my code. I thought my `next button` has `litleTextAttributes` or `label` property, but they don't. – M.Masa Dec 21 '17 at 02:52
  • @M.Masa In my knowledge, i think can not programmatically get `UIBarButtonItem` default font size. – motbantuangiauten Dec 21 '17 at 03:02
  • I see. Anyway your code was very helpful. Thank you. – M.Masa Dec 21 '17 at 10:59