iOS更改导航栏取消按钮的背景图像

时间:2013-09-06 01:50:25

标签: ios uinavigationcontroller uibarbuttonitem

我有一个从相机胶卷拉出的UIImagePickerController。

在导航栏中,有一个默认的UIBarButtonItem取消选择照片,但它根本不符合我的应用程序!

如何更改其外观甚至隐藏它,然后将自己的按钮放在导航栏中的位置?

使用“返回”按钮可以轻松完成此操作:

// Change the appearance of back button
UIImage *backButtonImage = [[UIImage imageNamed:@"back_button.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(20, 10, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

UIBarButtonItem似乎没有setCancelButtonBackgroundImage方法。

1 个答案:

答案 0 :(得分:1)

这个怎么样:

UIImage * cancelButtonImage = [UIImage imageNamed:@"cancel_button.png"];
UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setFrame:(CGRect){CGPointZero, hamburgerImage.size}];
[cancelButton setImage:cancelButtonImage forState:UIControlStateNormal];
[cancelButton addTarget:self
                 action:@selector(cancel:)
       forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
[self.navigationItem setLeftBarButtonItem:cancelButtonItem];