簡體   English   中英

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

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

如何在界面中使用自定義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次

您可以將其添加到單例全局變量文件中,例如

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM