0

我有一个带有后退按钮和视图名称的导航栏的详细视图。导航栏以编程方式设置。显示的名称是这样设置的。

self.title = NSLocalizedString(name, @"");

名称取决于呈现的视图。

现在我还想在导航栏上显示一个小图标,这也取决于视图。

我怎么做?

4

4 回答 4

4

您可以将背景图像设置为导航栏使用此

投放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;
于 2012-11-21T08:03:28.553 回答
0

将此代码添加到viewDidLoad您的 ViewController 中:

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 也可以声明为实例变量,因此您可以在添加后访问它(例如,如果您想在某个时候从导航栏中删除图标)。

于 2012-11-21T08:01:22.223 回答
0

您需要继承 UINavigationBar。然后在 drawRect 中执行以下操作:

[[UIImage imageNamed...] drawInRect:...]

并调用超级方法当然

于 2012-11-21T09:36:04.750 回答
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];
于 2013-04-29T06:06:50.990 回答