我有一个带有导航栏的详细视图,后面带有后退按钮和视图名称。 导航栏以编程方式设置。 显示的名称设置如下。
self.title = NSLocalizedString(name, @"");
名称取决于所呈现的视图。
现在我想在导航栏上显示一个小图标,这也取决于视图。
我该怎么做?
答案 0 :(得分:4)
您可以将backGround图像设置为navigationBar 使用此
加入didFinishLaunchingWithOptions
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title_bar.png"] forBarMetrics:UIBarMetricsDefault];
或者您可以使用
在任何视图中设置导航栏图像UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *image = [UIImage imageNamed:@"TopBar.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
或者您可以在NavigationBar上设置视图 Link
[[UINavigationBar appearance] addSubview:yourView];
或
self.navigationItem.titleView = YourView;
使用h
设置标题self.navigationItem.title = @"Your Title";
你可以使用这个
获得navigationBarButton-(void)getRightBarBtn
{
UIButton *Btn =[UIButton buttonWithType:UIButtonTypeCustom];
[Btn setFrame:CGRectMake(0.0f,0.0f,68.0f,30.0f)];
[Btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"yourImage.png"]] forState:UIControlStateNormal];
//[Btn setTitle:@"OK" forState:UIControlStateNormal];
//Btn.titleLabel.font = [UIFont fontWithName:@"Georgia" size:14];
[Btn addTarget:self action:@selector(yourBtnPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithCustomView:Btn];
[self.navigationItem setRightBarButtonItem:addButton];
}
在导航栏上设置简单的imageView
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"star.png"]];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:image];
self.navigationItem.rightBarButtonItem = backBarButton;
答案 1 :(得分:0)
将此代码添加到ViewController中的viewDidLoad
:
UIImage *image = [UIImage imageNamed: @"myIcon.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(5, 2, 100, 39);
[self.navigationController.navigationBar addSubview:imageView];
imageView也可以声明为实例变量,因此您可以在添加后访问它(例如,如果您想在某些时候从导航栏中删除图标)。
答案 2 :(得分:0)
您需要继承UINavigationBar。然后在drawRect中执行以下操作:
[[UIImage imageNamed...] drawInRect:...]
并调用超级方法
答案 3 :(得分:0)
UIButton *homeBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 46, 28)];
[homeBtn setBackgroundImage:[UIImage imageNamed:@"homeBtn.png"] forState:UIControlStateNormal];
[homeBtn addTarget:self action:@selector(homeBtnPressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *navHomeBtn=[[[UIBarButtonItem alloc] initWithCustomView:homeBtn] autorelease];
self.navigationItem.rightBarButtonItem=navHomeBtn;
[homeBtn release];