自定義導航欄上的按鈕

一、在導航欄左邊自定義一個按鈕ide

UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
    
    leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [leftButton setTitleColor:UIColorFromRGB(0x19191a) forState:UIControlStateNormal];
    [leftButton setTitle:@"帳單" forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(didList:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButtonItems = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    
    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
//    nagetiveSpacer.width = -15;//這個值能夠根據本身須要本身調整
    self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftBarButtonItems];

二、導航欄右邊自定義兩個按鈕code

UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 15, 20, 17)];
    
    rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButton setTitleColor:UIColorFromRGB(0x19191a) forState:UIControlStateNormal];
    [rightButton setImage:IMAGENAMED(@"storehouse") forState:UIControlStateNormal];
//    [leftButton setTitle:@"帳單" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(didList:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItems = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    
//    UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
//                                                                                   target:nil action:nil];
//    nagetiveSpacer.width = 40;//這個值能夠根據本身須要本身調整
//    self.navigationItem.rightBarButtonItems = @[nagetiveSpacer, rightBarButtonItems];
    
    UIButton *rightButtonMore = [[UIButton alloc] initWithFrame:CGRectMake(220, 15, 20, 17)];
    
    rightButtonMore.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightButtonMore setTitleColor:UIColorFromRGB(0x19191a) forState:UIControlStateNormal];
    [rightButtonMore setImage:IMAGENAMED(@"Treasure_more") forState:UIControlStateNormal];
    //    [leftButton setTitle:@"帳單" forState:UIControlStateNormal];
    [rightButtonMore addTarget:self action:@selector(clickMore:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarButtonItemsMore = [[UIBarButtonItem alloc] initWithCustomView:rightButtonMore];
    
    UIBarButtonItem *nagetiveSpacerMore = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                   target:nil action:nil];
    nagetiveSpacerMore.width = 0;//這個值能夠根據本身須要本身調整
    self.navigationItem.rightBarButtonItems = @[nagetiveSpacerMore, rightBarButtonItemsMore, rightBarButtonItems];

相關文章
相關標籤/搜索