您的位置:首页 > 产品设计 > UI/UE

UINavigationItem的那点事

2015-11-03 11:36 591 查看
有些时候在设置UINavagationItem的时候我也许会用到自己的图片来设置leftBarButtonItem和rightBarButtonItem,或者是标题。在这不啰嗦了,直接上代码示例:
// 用自定义的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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息