0

I encountered a strange problem on the rightBarButtonItem of UINavigationController. The margin of the button disappears on iOS7, so it looks like this:

enter image description here

At first, I thought it was some mistakes in my UINavigationController related categories, but I removed all my customisation code and all the header files of categories, and simply use a UINavigationController and an empty view controller.

In - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

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

In VCTestViewController.m -> viewDidLoad

self.view.backgroundColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"hello" style:UIBarButtonItemStyleBordered target:nil action:nil];

The problem still exists(as shown above). I can't figure out what is wrong. Do you have any idea about this problem?

Henry H Miao
  • 3,420
  • 4
  • 20
  • 26

2 Answers2

2

you can share your line of code if it's not working:

- (UIBarButtonItem *)rightNavBarButton
{
    UIButton *filterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [filterBtn setTitle:@"hello" forState:UIControlStateNormal];
    filterBtn.frame = CGRectMake(0, 0,40,27);
    [filterBtn addTarget:self action:@selector(getFriendsList) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *filterNavBarItem = [[UIBarButtonItem alloc] initWithCustomView:filterBtn];
    return filterNavBarItem;
}
Guntis Treulands
  • 4,764
  • 2
  • 50
  • 72
Shubham
  • 570
  • 3
  • 12
  • Thanks, this works but it's not what I expect. This problem doesn't occur in my other projects, and I have removed every extra code, but this problem still exists. I'd like to figure out why this happens. – Henry H Miao Feb 25 '14 at 08:49
  • Have you set any navigation title view ?. add background image and check the co-ordinate of UIBarButton Item – Shubham Feb 25 '14 at 08:57
0

I finally figure out what happened.

The link given by βhargavḯ is not the solution of my problem, but it's the cause of my problem. One of my colleagues wrote a category of UINavigationItem to narrow the empty space in iOS7. He then used method swizzling to exchanged the system method setRightBarButtonItem: with his own method. I didn't realise this.

So I think method swizzling would be extremely dangerous if you exchange the system methods with your own methods.

Henry H Miao
  • 3,420
  • 4
  • 20
  • 26