简体   繁体   English

如何在界面中使用自定义UIBarButtonItem都使用自定义UINavigationBar

[英]how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar

how to use custom UIBarButtonItem in interface whice all use custom UINavigationBar 如何在界面中使用自定义UIBarButtonItem都使用自定义UINavigationBar

Not written again in every interface. 没有在每个接口中再次写入。

Is there a unified method to uniform setting? 是否有统一的统一设置方法?

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];

A lot of interface backBarButtonItem need custom,code is the same,I don't want to write N times 很多接口backBarButtonItem需要自定义,代码是一样的,我不想写N次

You can add this in a singleton global-variables file, like 您可以将其添加到单例全局变量文件中,例如

GlobalVariable.h GlobalVariable.h

+(UIBarButtonItem*)customButton;

GlobalVariable.m 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;
}

Then, import GlobalVariable.h in the views you want the button to appear and call 然后,在您希望按钮出现的视图中导入GlobalVariable.h并调用

self.navigationItem.leftBarButtonItem = [GlobalVariable customButton];

You can keep GlobalVariable there, in case you need other variables too. 如果还需要其他变量,则可以在此处保留GlobalVariable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM