1

我有 UINavigarionController,我想在导航栏中添加自定义背景和徽标以及左按钮我可以添加背景但我的徽标和按钮没有出现,请您帮助我,提前致谢!

4

3 回答 3

0

您不能在 AppDelegate 中创建

打开你的 ViewController.m

- (void)viewDidLoad
 {
    self.navigationItem.title=@"Title"; //Set the title
    [self CustomButton];
    [super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}

-(void) CustomButton
{
    UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];//create a button
    but.frame=CGRectMake(290, 4, 40, 40);// set the button frame size
    [but setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
    [but addTarget:self action:@selector(call here) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:but];
    self.navigationItem.rightBarButtonItem = btn;
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

}

希望它对你有用:)

于 2013-03-21T08:46:01.947 回答
0

barButton 不能设置为在整个应用程序中使用。

每个都ViewController提供自己的,barButtonItems因此您必须将设置徽标和按钮的代码移动到 rootViewController 类titleViewnavigationItem

于 2013-03-21T08:38:33.373 回答
0

您的代码有效,我得到了徽标和按钮,但进行了一次更改

  UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
 [button setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
  //[button addTarget:self action:@selector(parametres:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 25, 25)];

 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightButton;

UIImageView *navigationImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
navigationImage.image=[UIImage imageNamed:@"menu.png"];
UIImageView *workaroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 30)];
[workaroundImageView addSubview:navigationImage];

self.navigationItem.titleView= workaroundImageView;
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menu.png"]];

只需将图像类型(即不是菜单)指定为 menu.png 并通过将 UINavigationControllerDelegate 设置为您的类来将其添加到您的导航项中,如上祝您好运,请分享您的结果

于 2013-03-21T08:18:31.393 回答