iOS7之前的UI为:

而在iOS7中,由于设计方面的原因,使得UI变为:

修改的方法重写UINavigationItem的setLeftBarButtonItem和setRightBarButtonItem方法,使之与之前版本兼容;

代码如下:

UINavigationItem+margin.h

1
2
3
4
5
6
7
8
9
10
11
12
//
//  UINavigationItem+margin.h
//
//  Created by shjborage on 1/20/14.
//  Copyright (c) 2014 Saick. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UINavigationItem (margin)

@end

UINavigationItem+margin.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
//  UINavigationItem+margin.m
//
//  Created by shjborage on 1/20/14.
//  Copyright (c) 2014 Saick. All rights reserved.
//

#import "UINavigationItem+margin.h"

@implementation UINavigationItem (margin)

#if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1

- (void)setLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem
{
  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeperator.width = -10.0f;
    
    if (_leftBarButtonItem) {
      [self setLeftBarButtonItems:@[negativeSeperator, _leftBarButtonItem]];
    } else {
      [self setLeftBarButtonItems:@[negativeSeperator]];
    }
  } else {
    [self setLeftBarButtonItem:_leftBarButtonItem animated:NO];
  }
}

//- (void)setLeftBarButtonItems:(NSArray *)leftBarButtonItems
//{
//  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    negativeSeperator.width = -10.0f;
//    
//    if ([_leftBarButtonItems count] > 0) {
//      NSMutableArray *items = [NSMutableArray arrayWithArray:leftBarButtonItems];
//      [items insertObject:negativeSeperator atIndex:0];
//      [self setLeftBarButtonItems:items animated:NO];
//    } else {
//      [self setLeftBarButtonItems:leftBarButtonItems animated:NO];
//    }
//  } else {
//    [self setLeftBarButtonItems:leftBarButtonItems animated:NO];
//  }
//}

- (void)setRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem
{
  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSeperator.width = -10.0f;
    
    if (_rightBarButtonItem) {
      [self setRightBarButtonItems:@[negativeSeperator, _rightBarButtonItem]];
    } else {
      [self setRightBarButtonItems:@[negativeSeperator]];
    }
  } else {
    [self setRightBarButtonItem:_rightBarButtonItem animated:NO];
  }
}

//- (void)setRightBarButtonItems:(NSArray *)rightBarButtonItems
//{
//  if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
//    UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    negativeSeperator.width = -10.0f;
//    
//    if ([_leftBarButtonItems count] > 0) {
//      NSMutableArray *items = [NSMutableArray arrayWithArray:rightBarButtonItems];
//      [items insertObject:negativeSeperator atIndex:0];
//      [self setRightBarButtonItems:items animated:NO];
//    } else {
//      [self setRightBarButtonItems:rightBarButtonItems animated:NO];
//    }
//  } else {
//    [self setRightBarButtonItems:rightBarButtonItems animated:NO];
//  }
//}

#endif

@end

项目源码不做任何修改,问题解决;

添加Button:

1
2
3
UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = leftBarItem;
[leftBarItem release];

如果原来项目中有使用self.navigationItem.leftBarButtonItemsself.navigationItem.rightBarButtonItems可参考注释部分内容。如果都是多个的,那么直接换把注释解开,把单个的给注释掉。搞懂原理才是重要的

原文地址