隐藏导航项中的文本

时间:2013-11-26 12:10:20

标签: ios objective-c uiview uinavigationitem

我需要通过替换导航栏中的Text(按钮)来设置后退箭头。我可以查看后退箭头,但我不知道如何在导航项中隐藏后面的文本。这是我的代码:

我收到警告:: CGImageCreateWithImageProvider:图片大小无效:0 x 0。

(void)viewWillAppear:(BOOL)animated
 {
    UIImage *backButtonHomeImage = [[UIImage imageNamed:@"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];
    [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonHomeImage  forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

    self.navigationItem.leftBarButtonItem = nil;
}

5 个答案:

答案 0 :(得分:3)

答案

    -(void)viewWillAppear:(BOOL)animated
     {
        [super viewWillAppear:animated];
        if([self.navigationController.viewControllers objectAtIndex:0] != self)
        {
            UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 26, 26)];
            [backButton setImage:[UIImage imageNamed:@"back-arrow.png"] forState:UIControlStateNormal];
            [backButton setShowsTouchWhenHighlighted:TRUE];
            [backButton addTarget:self action:@selector(popViewControllerWithAnimation) forControlEvents:UIControlEventTouchDown];
            UIBarButtonItem *barBackItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
            self.navigationItem.hidesBackButton = TRUE;
            self.navigationItem.leftBarButtonItem = barBackItem;
        }
}

-(void)popViewControllerWithAnimation
{  
   [self.navigationController popViewControllerAnimated:YES];

}

可能会有所帮助。

答案 1 :(得分:1)

试试这个:

UIImage *backButtonHomeImage = [[UIImage imageNamed:@"back-arrow.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 104, 13)];

答案 2 :(得分:0)

在推送到该视图控制器之前,只需将空白文本设置为导航栏的标题,如下面的代码。

self.navigationItem.title=@" ";

答案 3 :(得分:0)

创建自定义后退按钮并将该按钮添加到导航控制器后退按钮。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0,0,54,32);
UIImage *backButtonImage = [UIImage imageNamed:@"back-arrow.png"];
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal];
[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];

答案 4 :(得分:0)

我希望这会起作用

 self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];