如何防止自定义导航栏后退按钮图像拉伸

时间:2015-12-01 14:06:16

标签: ios objective-c swift uinavigationcontroller

我使用以下代码在navigationButton中将自定义图像设置为我的后退按钮:

var backButtonImage : UIImage = UIImage(named: "back")!
       // backButtonImage = backButtonImage.resizableImageWithCapInsets(UIEdgeInsets(top: 0,left: 0 ,bottom: 0, right: 0), resizingMode: UIImageResizingMode.RawValue )
        UIBarButtonItem.appearance().setBackButtonBackgroundImage(backButtonImage, forState: .Normal, barMetrics: .Default)

问题是它会像这样拉伸导航栏按钮图像:

enter image description here

我应该如何停止自动拉伸图像?

由于

1 个答案:

答案 0 :(得分:0)

你可以在Objective C上这样做,首先单独创建一个按钮,然后将其添加到条形按钮

UIImage *menuImage = [UIImage imageNamed:@"icon-main-normal.png"];
        UIButton *menu = [UIButton buttonWithType:UIButtonTypeCustom];
        menu.bounds =CGRectMake( 0, 0, 30, 30 );
        [menu setImage:menuImage forState:UIControlStateNormal];
        [menu addTarget:self action:@selector(backToHome) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithCustomView:menu];

        self.navigationItem.leftBarButtonItem = menuBtn;

你可以通过这个逻辑

在swift上做到这一点