我正在尝试创建UINavigationController
的可重用子类。所以这是我的代码:
@interface MainNavigationController : UINavigationController
...
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBar.barTintColor = primaryOrange;
self.navigationBar.tintColor = white;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:nil];
}
因此导航栏的颜色正在改变但是没有出现按钮的问题 我错过了什么?
更新 我希望在我的图像中为NavigationController提供相同的NavigationController(按钮和颜色...):
更新 所以我用这种方式将navugationController子类化:
-(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(showMainMenu)];
}
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
if (self) {
rootViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:UIBarButtonItemStylePlain target:self action:nil];
rootViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(showMainMenu)];
}
return self;
}
这项工作但唯一的问题是,当我按下视图而不是左边有后退按钮时,我会得到始终搜索图标,如何修复它(我想在左边有父级搜索按钮,后面有按钮孩子的观点)?
答案 0 :(得分:2)
子类设计应该是这样的 -
void CSelectTestDefinitionDlg::OnNMCustomdraw(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMLVCUSTOMDRAW lpLVCustomDraw = reinterpret_cast<LPNMLVCUSTOMDRAW>(pNMHDR);
switch (lpLVCustomDraw->nmcd.dwDrawStage)
{
case CDDS_ITEMPREPAINT:
case CDDS_SUBITEM:
if (lpLVCustomDraw->nmcd.uItemState & CDIS_SELECTED)
{
// Your color definitions here:
lpLVCustomDraw->clrText = RGB(255, 255, 255);
lpLVCustomDraw->clrTextBk = RGB(0, 70, 60);
}
break;
default:
break;
}
*pResult = 0;
*pResult |= CDRF_NOTIFYPOSTPAINT;
*pResult |= CDRF_NOTIFYITEMDRAW;
*pResult |= CDRF_NOTIFYSUBITEMDRAW;
}
按要求提供完整代码 -
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
if (self) {
[self setLeftButtonToController:rootViewController];
}
return self;
}
- (void)setLeftButtonToController:(UIViewController *)viewController {
UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
[viewController.navigationItem setRightBarButtonItem:closeItem];
}
如何使用 -
//
// MyOwnNavigationController.h
//
//
#import <UIKit/UIKit.h>
@interface MyOwnNavigationController : UINavigationController
- (id)initWithRootViewController:(UIViewController *)rootViewController;
@end
//
// MyOwnNavigationController.m
//
//
#import "MyOwnNavigationController.h"
@interface MyOwnNavigationController ()
@end
@implementation MyOwnNavigationController
- (id)initWithRootViewController:(UIViewController *)rootViewController {
self = [super initWithRootViewController:rootViewController];
if (self) {
[self setLeftButtonToController:rootViewController];
}
return self;
}
- (void)setLeftButtonToController:(UIViewController *)viewController {
UIBarButtonItem *closeItem = [[UIBarButtonItem alloc] initWithTitle:@"Close" style:UIBarButtonItemStylePlain target:self action:@selector(dismissController)];
[viewController.navigationItem setRightBarButtonItem:closeItem];
}
@end
对于StoryBroad的更新 ---- 将以下方法添加到MyOwnNavigationController.m
TwoViewController * viewController = [[TwoViewController alloc] initWithNibName:@"TwoViewController" bundle:nil];
MyOwnNavigationController *navController = [[MyOwnNavigationController alloc] initWithRootViewController:viewController];