NavigationBar上的常见BarButtonItems

时间:2019-06-08 16:30:13

标签: ios swift uinavigationcontroller uinavigationbar

我有10个ViewController,其中有NavigationBar,每个VC具有相同的3个Barbutton项目(左侧1个,右侧2个)。现在,我在每个ViewController中添加了相同的按钮,因此我在10个控制器中重复了相同条形按钮项目的代码。

在这种情况下正确的方法是什么?我曾考虑过将UIViewController用作基础,并在BarButtonItem上添加这些NavigationBar,但是由于我正在使用Coordinator进行导航,因此每个路径都存在问题,那3个BarButtonItem导航到其他屏幕。同样,我也尝试了UIViewController扩展名,但仍然存在导航问题。

1 个答案:

答案 0 :(得分:1)

目标C

在一个通用类中声明这些方法,我有Utilities.h

+ (NSArray *)setButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action;
+ (NSArray *)setButtonWithImage:(NSString *)imgName target:(id)target action:(SEL)action;
+ (NSArray *)setTwoButtonWithTarget:(id)target action1:(SEL)action1 image1:(NSString *)imgName1 action2:(SEL)action2 image2:(NSString *)imgName2;

然后在相应文件中定义这些方法。因为我有Utilities.m

+ (NSArray *)setButtonWithTitle:(NSString *)title target:(id)target action:(SEL)action {

     UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
     negativeSpace.width = -10;
     UIBarButtonItem *textBarButton = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];

     NSArray *arrBarItems = [NSArray arrayWithObjects:negativeSpace, textBarButton, nil];
     return arrBarItems;
}

+ (NSArray *)setButtonWithImage:(NSString *)imgName target:(id)target action:(SEL)action {

     UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpace.width = -5;
     UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imgName] style:UIBarButtonItemStylePlain target:target action:action];

    NSArray *arrBarItems = [NSArray arrayWithObjects:negativeSpace, backButton, nil];
     return arrBarItems;
}

+ (NSArray *)setTwoButtonWithTarget:(id)target action1:(SEL)action1 image1:(NSString *)imgName1 action2:(SEL)action2 image2:(NSString *)imgName2 {

     UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpace.width = -5;

     UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imgName1] style:UIBarButtonItemStylePlain target:target action:action1];
    UIBarButtonItem *barButton2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:imgName2] style:UIBarButtonItemStylePlain target:target action:action2];

     NSArray *arrBarItems = [NSArray arrayWithObjects:negativeSpace, barButton2, barButton1, nil];
    return arrBarItems;
}

如果您需要对按钮进行更多自定义,例如bgcolor,titlecolor等,请使用上述方法中的n种。然后使用UIButton并将其与barbuttonitem一起使用。

现在,在每个ViewController中,您只需要放置两行,一行用于左键,另一行用于右键。

self.navigationItem.leftBarButtonItems = [Utilities setButtonWithImage:@"ic_back" target:self action:@selector(goBackToPreviousView:)];
self.navigationItem.rightBarButtonItems = [Utilities setTwoButtonWithTarget:self action1:@selector(showSearchBar:) image1:@"ic_search" action2:@selector(showSortFilters:) image2:@"ic_sort"];

快速

class Utilities {

    class func setButtonWith(title:String, target:Any, action:Selector) -> [UIBarButtonItem] {

        let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target:nil, action:nil)
        negativeSpace.width = -10;
        let textBarButton = UIBarButtonItem(title:title, style:UIBarButtonItem.Style.plain, target:target, action:action)

        let arrBarItems = [negativeSpace, textBarButton]
        return arrBarItems
    }

    class func setButtonWith(imgName: String, target:Any, action:Selector) -> [UIBarButtonItem] {

        let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target:nil, action:nil)
        negativeSpace.width = -5;
        let backButton = UIBarButtonItem(image:UIImage(named:imgName), style:UIBarButtonItem.Style.plain, target:target, action:action)

        let arrBarItems = [negativeSpace, backButton]
        return arrBarItems
    }

    class func setTwoButtonWith(target:Any, action1:Selector, imgName1:String, action2:Selector, imgName2:String)  -> [UIBarButtonItem] {

        let negativeSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.fixedSpace, target:nil, action:nil)
        negativeSpace.width = -5;

        let barButton1 = UIBarButtonItem(image:UIImage(named:imgName1), style:UIBarButtonItem.Style.plain, target:target, action:action1)
        let barButton2 = UIBarButtonItem(image:UIImage(named:imgName2), style:UIBarButtonItem.Style.plain, target:target, action:action2)

        let arrBarItems = [negativeSpace, barButton2, barButton1]
        return arrBarItems
    }
}

在任何需要的地方,都可以这样称呼它:

self.navigationItem.leftBarButtonItems = Utilities.setButtonWith(imgName: "ic_back", target: self, action: #selector(goBackToPreviousView))
self.navigationItem.rightBarButtonItems = Utilities.setTwoButtonWith(target: self, action1: #selector(showSearchBar), imgName1: "ic_search", action2: #selector(showSortFilters), imgName2: