在导航控制器类的类别中添加左右栏按钮项

时间:2014-01-06 12:15:20

标签: ios iphone

我已经创建了一个导航控制器类别并设置了标题,左右边栏按钮在视图中加载了此导航控制器类(此类的类别)。

- (void)viewDidAppear:(BOOL)animated
{

        NSArray *ver = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];
        if ([[ver objectAtIndex:0] intValue] >= 7) {
            self.navigationBar.barTintColor = [UIColor colorWithRed:229.0f/255.0f green:114.0f/255.0f blue:89.0f/255.0f alpha:1.0f];
            self.navigationBar.translucent = NO;
        }else{
            self.navigationBar.tintColor = [UIColor colorWithRed:229.0f/255.0f green:114.0f/255.0f blue:89.0f/255.0f alpha:1.0f];
        }

        UILabel *lblTitleView=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 170, 44)];
        [lblTitleView setTextColor:[UIColor whiteColor]];
        [lblTitleView setFont:[UIFont boldSystemFontOfSize:17]];
        [lblTitleView setText:[self SetnavigationTitle:self.navigationItem.title]];
        [lblTitleView setTextAlignment:NSTextAlignmentCenter];
        [lblTitleView setBackgroundColor:[UIColor clearColor]];
        [self.navigationItem setTitleView:lblTitleView];

       // UINavigationController*navCnt = [[UINavigationController alloc] init];

      UIButton *btnLeft=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnLeft setFrame:CGRectMake(0, 5, 32, 32)];
        [btnLeft setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
        [btnLeft setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
         [btnLeft setTitle:[NSString stringWithFormat:@"%@",[self SetleftbarbuttonTitle:self.navigationItem.leftBarButtonItem.title]] forState:UIControlStateNormal];
        [btnLeft setTintColor:[UIColor grayColor]];
        [btnLeft setBackgroundColor:[UIColor greenColor]];

        [btnLeft setTag:101];
        [btnLeft addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *leftBarButton=[[UIBarButtonItem alloc]initWithCustomView:btnLeft];
        [self.navigationItem  setLeftBarButtonItem:leftBarButton];
        self.navigationItem.hidesBackButton=YES;


       UIButton *btnRight=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnRight setFrame:CGRectMake(270, 5, 32, 32)];
        [btnRight setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
        [btnRight setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateHighlighted];
        [btnRight setTitle:[NSString stringWithFormat:@"%@",[self SetRightbarbuttonTitle:self.navigationItem.rightBarButtonItem.title]] forState:UIControlStateNormal];
        [btnRight setTintColor:[UIColor clearColor]];
        [btnRight setBackgroundColor:[UIColor greenColor]];


         [btnRight setTag:102];
        [btnRight addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *rightBarButton=[[UIBarButtonItem alloc]initWithCustomView:btnRight];

        [self.navigationItem setRightBarButtonItem:rightBarButton];

        [super viewDidAppear:animated];


}

当我从任何视图控制器传递标题时,它的工作正常,但每个视图控制器 现在我有了在导航控制器子类中设置左右栏按钮项的代码。

但现在在视图控制器上我没有在此视图控制器上获得导航控制器的uibar按钮项。  提前谢谢。

3 个答案:

答案 0 :(得分:7)

试试这个:

显示导航控制器的 leftBarButtonItem

    UIImage *faceImage = [UIImage imageNamed:@"back_arrow.png"];
    UIButton *face = [UIButton buttonWithType:UIButtonTypeCustom];
    face.bounds = CGRectMake( 10, 0, faceImage.size.width, faceImage.size.height );
    [face addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventTouchUpInside];
    [face setImage:faceImage forState:UIControlStateNormal];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:face];
    self.navigationItem.leftBarButtonItem = backButton;
    [self.navigationItem setHidesBackButton:YES animated:YES];
    [self.navigationItem setLeftBarButtonItem:nil animated:NO];
    [self.navigationItem setBackBarButtonItem:nil];

显示导航控制器的 rightBarButtonItem

UIImage *faceImage1= [UIImage imageNamed:@"slideshowarrow"];
UIButton *face1 = [UIButton buttonWithType:UIButtonTypeCustom];
face1.bounds = CGRectMake( 10, 0, faceImage1.size.width, faceImage1.size.height );
[face1 addTarget:self action:@selector(Goto_nextView) forControlEvents:UIControlEventTouchUpInside];
[face1 setImage:faceImage1 forState:UIControlStateNormal];
UIBarButtonItem *backButton1 = [[UIBarButtonItem alloc] initWithCustomView:face1];
self.navigationItem.rightBarButtonItem = backButton1;

答案 1 :(得分:3)

设置按钮框架的工作原理或使用下面的代码左右按钮。它的工作原理是100%

UIImage* buttonImage = [UIImage imageNamed: @"header.navigation.back.png"]; 
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame = CGRectMake(0.0, 0.0, buttonImage.size.width/2, 32);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:aButton];
[aButton addTarget:self action:@selector(backToPriorView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = backButton;

答案 2 :(得分:0)

Swift 4.0

let btnBack = UIButton(type: .custom)
btnBack.bounds = CGRect(x: 0, y: 0, width: 44, height: 44)
btnBack.addTarget(self, action: #selector(self.backButtonAction(sender:)), for: .touchUpInside)
btnBack.setImage(#imageLiteral(resourceName: "back_image"), for: .normal)

let leftBarButton = UIBarButtonItem(customView: btnBack)
self.navigationItem.setLeftBarButton(leftBarButton, animated: true)

//后退按钮操作

@objc func backButtonAction(sender: UIButton) {
    self.navigationController?.popViewController(animated: true)
}