0

I tried to make a navigation back button which has the same complete style as system, but no matter how I adjust it still had a little difference.

  1. system
    system

  2. custom
    custom

You can see no matter the arrow or the word (返回 BACK) both have some difference.

Here is the code I tried and I got the arrow image from here.

    UIImage *sourceImage = [UIImage imageNamed:@"nav-back-btn"];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypePlain];
    [backBtn setImage:sourceImage forState:UIControlStateNormal];
    [backBtn setTitle:@" 返回" forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(goPreviosPage) forControlEvents:UIControlEventTouchUpInside];
    backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];

    backBtn.transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(-8, 0), 1.1, 1.1);
ovo
  • 1,904
  • 13
  • 26

1 Answers1

0

Use the default UINavigationcontroller strategy.

  1. Add a navigation controller and its view controllers.
  2. Give both view controller navigation title. self.navigationItem.title in view did load.
  3. Just pushing the navigation controller will default have a back button and the title of the previous view controller to it with same system stle.
  4. Do not set any custom leftbarbutton item as this may overide the UINAvigationcontroller to show the default back button.

Hope this help.

codelover
  • 1,113
  • 10
  • 28
  • I'll control the back button to show or not whether it is default or my custom, therefore if you know how to set the appearance is the same as the default back button, just tell me. – ovo Jun 30 '17 at 09:26
  • Well i hope i understand your comment check or else explain me clearly. The appearance will just take the same as the existing UInavbar button and bar. The default back button wll only have "<" if no title set for previous view controller or else it will be "< TitleofpreviewViewController" if you set the title as i explained in my answer. – codelover Jun 30 '17 at 10:12
  • I know what you say, but I want and you say are two things. I just want my custom back button do my custom action and its appearance same as system back button (< BACK). – ovo Jun 30 '17 at 15:38
  • then just add uibutton and customise it , add target to it and add that to nav item. this will solve your purpose – codelover Jul 08 '17 at 08:13