导航栏按钮隐藏当动画设置为YES时

时间:2013-05-01 13:51:49

标签: iphone ios uinavigationcontroller uinavigationbar uinavigationitem

我将Back和Next按钮设置为导航栏项目左按钮和右按钮。下面的代码为

 //  Back Button

UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

   [aButton addTarget:self action:@selector(navigatehome)forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.leftBarButtonItem = aBarButtonItem;

//Next Button
 aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:aButton];

[aButton addTarget:self action:@selector(navigatenext)forControlEvents:UIControlEventTouchUpInside];

[self.navigationItem setRightBarButtonItem:aBarButtonItem]

语句中的animated:YES时出现问题

  CSA_info *h1=[[CSA_info alloc]init];
[self.navigationController pushViewController:h1 animated:YES];

如下图所示

image when button not shown

如果使用animated:No,则显示如下

enter image description here

我在导航栏中将子图像添加为子视图,因此它隐藏了按钮。

 UIImage *image = [UIImage imageNamed:@"title bar.png"];
 UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
 imageView.frame = CGRectMake(-5, 0, 330, 44); 
 [self.navigationController.navigationBar insertSubview:imageView atIndex:2];

我需要在导航中添加该图像。

为什么会这样,任何帮助?

2 个答案:

答案 0 :(得分:1)

如果我理解你的问题,那么以下解决方案应该有效,

您需要在视图控制器的viewDidLoad方法中添加代码。请参考以下代码

UIButton *backButton = [[UIButton alloc] initWithFrame:kButtonFrame];
[backButton setImage:[UIImage imageNamed:kBackButtonImage] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(navigatehome)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = aBarButtonItem;

UIButton *nextButton = [[UIButton alloc] initWithFrame:kButtonFrame];
[nextButton setImage:[UIImage imageNamed:kNextButtonImage] forState:UIControlStateNormal];
[nextButton addTarget:self action:@selector(navigatenext)forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *aBarButtonItemRight = [[UIBarButtonItem alloc] initWithCustomView:nextButton];
[self.navigationItem setRightBarButtonItem:aBarButtonItemRight];

即使你在推动视图控制器时动画也应该可以正常工作。

答案 1 :(得分:1)

你需要将标题栏图像放在secondViewcontroller中,如下面的ViewDidLoad

UIImage* imageback = [UIImage imageNamed:@"Back_Button.png"];
                CGRect frameimgback = CGRectMake(0, 0, 50, 30);
                backButton = [[UIButton alloc] initWithFrame:frameimgback];
                [backButton setBackgroundImage:imageback forState:UIControlStateNormal];
                [backButton addTarget:self action:@selector(Back)
                     forControlEvents:UIControlEventTouchUpInside];

                UIImage *image = [UIImage imageNamed:@"titileBar.png"];
                [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

                UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithCustomView:backButton];
                self.navigationItem.leftBarButtonItem = btn;