导航栏上的自定义后退按钮不起作用

时间:2015-07-17 10:44:12

标签: ios uinavigationbar back-button

我使用了以下代码,而不是显示我在内置箭头图像中显示的图像

 //Set Back button on Navigation
    [[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"backBtn.png"]];
    [[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backBtn.png"]];

2 个答案:

答案 0 :(得分:1)

希望这可以帮助你..

[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"backBtn.png"]
                                                  forState:UIControlStateNormal
                                                barMetrics:UIBarMetricsDefault];

答案 1 :(得分:1)

UIBarButtonItem *leftButtonItem = [self barItemWithTitle:@"Back" xOffset:11 target:self action:nil];
self.navigationItem.leftBarButtonItem = leftButtonItem;

下面的方法会返回导航栏的UIBarButtonItem

- (UIBarButtonItem*)barItemWithTitle:(NSString*)title xOffset:(NSInteger)xOffset target:(id)target action:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [button addTarget:target action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:18.0f]}];
    CGSize adjustedSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
    CGRect rect = CGRectMake(0, 0,adjustedSize.width + 3,24);
    [button setFrame:rect];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
        [button setContentEdgeInsets:UIEdgeInsetsMake(0, xOffset, 0, -xOffset)];
    }

    UIBarButtonItem *customUIBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    return customUIBarButtonItem;
}

行动方法

-(void)buttonAction{
     //Your code goes here.
}

愿这个有用了。