如何在NavBar中以编程方式设置图片? [英] How to set a picture programmatically in a NavBar?
问题描述
我有一个带有导航栏的详细视图,后面带有后退按钮和视图名称。
导航栏以编程方式设置。
显示的名称设置如下。
self.title = NSLocalizedString(name,@);
该名称取决于所呈现的视图。
现在我想呈现一个小的导航栏上的图标也取决于视图。
我该怎么做?
放入 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外观] addSubview:yourView];
或
self.navigationItem.titleView = YourView;
设置标题使用h
self.navigationItem.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:@OKforState:UIControlStateNormal];
//Btn.titleLabel.font = [UIFont fontWithName:@Georgiasize: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;
I have a detailview with a navigation bar with a back button and a name for the view. The navigation bar is set programmatically. The name presented is set like this.
self.title = NSLocalizedString(name, @"");
The name depends on the presented view.
Now I would like to also present a small icon on the navigation bar which also is depends on the view.
How do I do that?
You can set backGround image to navigationBar Using this
Put in didFinishLaunchingWithOptions
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"title_bar.png"] forBarMetrics:UIBarMetricsDefault];
Or you can set navigation bar image in any view using
UINavigationBar *navBar = [[self navigationController] navigationBar];
UIImage *image = [UIImage imageNamed:@"TopBar.png"];
[navBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
Or you can set a view on your NavigationBar Link
[[UINavigationBar appearance] addSubview:yourView];
or
self.navigationItem.titleView = YourView;
And set title using h
self.navigationItem.title = @"Your Title";
And you can get navigationBarButton using this
-(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];
}
And set simple imageView on navigation Bar
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"star.png"]];
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:image];
self.navigationItem.rightBarButtonItem = backBarButton;
这篇关于如何在NavBar中以编程方式设置图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!