【问题标题】:Custom back button on navigation bar Not working导航栏上的自定义后退按钮不起作用
【发布时间】:2015-07-17 10:44:11
【问题描述】:

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

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

【问题讨论】:

标签: ios uinavigationbar back-button


【解决方案1】:

希望对你有所帮助..

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

【讨论】:

【解决方案2】:
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.
}

希望对您有帮助。

【讨论】:

  • 我会检查一下,伙计
  • @nischalhada 毫无疑问它正在工作,因为我正在使用它。
  • 我已经解决了与我发布的问题相同的问题,感谢您的努力。
  • 关于这个问题的任何想法stackoverflow.com/questions/31481559/…
猜你喜欢
  • 1970-01-01
  • 2012-01-03
  • 1970-01-01
  • 2016-04-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 2020-01-07
  • 1970-01-01
相关资源
最近更新 更多