gpt4 book ai didi

ios - 在导航 Controller 类的类别中添加左右栏按钮项

转载 作者:行者123 更新时间:2023-12-01 17:20:38 31 4
gpt4 key购买 nike

我已经创建了一个导航 Controller 类别并在 View 中设置标题和左右栏按钮确实加载了这个导航 Controller 类(这个类的类别)。

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


}

当我只是从任何 View Controller 传递标题时,它对每个 View Controller 都可以正常工作,但是
现在我有在导航 Controller 子类中设置左右栏按钮项的代码。

但现在在 View Controller 上,我没有在这个 View Controller 上获得导航 Controller 的 uibar 按钮项。
提前致谢。

最佳答案

试试这个:

展示 leftBarButtonItem 导航 Controller :

    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 导航 Controller :
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;

关于ios - 在导航 Controller 类的类别中添加左右栏按钮项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949844/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com