我有一个问题,我现在无法找到解决方案。在我的应用程序中,我的导航栏中有多个按钮,这些按钮在整个应用程序中都是必需的,而不是在每个视图控制器中创建按钮,我想创建一个UINavigationBar或UINavigationController的子类(我不知道哪一个)。因此,无论何时用户在视图之间移动,导航栏始终包含这些按钮。到目前为止,我一直在搜索这个但是没有找到任何值得的东西。 请提前告诉我一个方法,谢谢。
答案 0 :(得分:0)
#import "CustomNavBar.h"
@implementation CustomNavBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.tintColor=[UIColor greenColor];
}
return self;
}
- (void)drawRect:(CGRect)rect {
UIImage *image = [UIImage imageNamed:@"Custom-Nav-Bar-BG.png"];
[image drawInRect:CGRectMake(0, 0, 40, self.frame.size.height)];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn drawRect:CGRectMake(42, 0, 40, self.frame.size.height )];
UIButton *btn2=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn2 drawRect:CGRectMake(82, 0, 40, self.frame.size.height )];
}
@end
答案 1 :(得分:0)
您可以subClass标准UINavigationBar
来实现此目标
@interface CustomNavigationBar : UINavigationBar
- (id)initWithFrame:(CGRect)frame;
@end
@implementation CustomNavigationBar
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
[btn addTarget:self action:@selector(<#selector#>) forControlEvents:<#(UIControlEvents)#>]
[self addSubview:btn];
...
...
}
return self;
}
- (void)drawRect:(CGRect)rect {
[[UIColor redColor] setFill];
UIRectFill(rect);
}
@end
要使用此StoryBoard
或xib
,只需将标准班级名称更改为CustomNavigationBar
。
或强>
如果您想以编程方式使用
在AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navVC = [[UINavigationController alloc] initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:@"VC"];
[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
}