如何在界面中使用自定义UIBarButtonItem全部使用自定义UINavigationBar
不在每个界面中再次写入。
统一设置是否有统一的方法?
UIImage image = [UIImage imageNamed:imagePath];
UIButton button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
很多接口backBarButtonItem需要自定义,代码是一样的,我不想写N次
答案 0 :(得分:0)
您可以在单个全局变量文件中添加它,例如
GlobalVariable.h
+(UIBarButtonItem*)customButton;
GlobalVariable.m
+(UIBarButtonItem*)customButton{
UIImage *image = [UIImage imageNamed:imagePath];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[button setImage:image forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:highLightImagePath] forState:UIControlStateHighlighted];
[button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* btn = [[UIBarButtonItem alloc] initWithCustomView:button];
return btn;
}
然后,在您希望按钮显示的视图中导入GlobalVariable.h
并调用
self.navigationItem.leftBarButtonItem = [GlobalVariable customButton];
如果您还需要其他变量,可以将GlobalVariable保留在那里。