导航栏后退按钮自定义图像y偏移

时间:2016-04-15 04:58:26

标签: ios uinavigationbar uinavigationitem

我将自定义后退按钮图像添加到UINavigationbar

func setBackButton()
{
    let backImg: UIImage = UIImage(named: "BackButton")!
    UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -60), forBarMetrics: .Default)

    UINavigationBar.appearance().tintColor = UIColor.goldenColor()
    UINavigationBar.appearance().backIndicatorImage = backImg
    UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImg
}

自定义图像设置正确但对齐不合适。后退按钮的y轴移动到下y轴。如何将其与导航栏的中间对齐。

我的back button image dimensions是34 * 9(1x),68 * 18(2x),135 * 36(3x)

enter image description here

1 个答案:

答案 0 :(得分:0)

请尝试设置后退按钮,如下所示

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
else
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

UIImage *btnImage = [UIImage imageNamed:@"back.png"];
[button setImage:btnImage forState:UIControlStateNormal];
[button setImage:btnImage forState:UIControlStateHighlighted];
button.frame = CGRectMake(0, 0, 30, 45);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];

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