有些时候在设置UINavagationItem的时候我也许会用到自己的图片来设置
leftBarButtonItem和right
BarButtonItem,或者是标题。在这不啰嗦了,直接上代码示例:
// 用自定义的view来替换标题
@property(nullable, nonatomic,strong) UIView *titleView;
// 左、右标题
@property(nullable, nonatomic,strong) UIBarButtonItem *leftBarButtonItem;
@property(nullable, nonatomic,strong) UIBarButtonItem *rightBarButtonItem;
// UIBarButtonItem父类(UIBarItem)的属性,用来修改item的位置
@property(nonatomic) UIEdgeInsets imageInsets; // default is UIEdgeInsetsZero
// 示例
// 左右标题位置设置
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imageName] style:UIBarButtonItemStylePlain target:self action:nil];
leftItem.imageInsets = UIEdgeInsetsMake(0, 0, 12, 0); // UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
self.navigationItem.leftBarButtonItem = leftItem;
// titleView位置,在这有个专门的方法来设置他的上下位置
- (void)setTitleVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics;
// 这里我是用[UINavigationBar appearance]调出程序所有的UINavigationController 在appdaelegate.m 里面同一添加设置(UINavigationController通用的属性都可以统一设置)
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-12 forBarMetrics:UIBarMetricsDefault];