更改UIBarButtonItem后退按钮

时间:2013-11-27 08:38:42

标签: ios uinavigationcontroller uibarbuttonitem

我正在尝试更改导航控制器backBarButton, 用out创造新的功能。 这是我的代码:

UIImage *backButtonImage = [[UIImage imageNamed:@"backt.clicked@2x~iphone.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 27, 0, 27)];

(宽度= 27,高= 27);

 [[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

这就是我得到的:

enter image description here

如何更改尺寸并删除标题?

2 个答案:

答案 0 :(得分:0)

- (UIBarButtonItem *)createBackMenuButton {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton addTarget:self action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setFrame:BACK_BUTTON_FRAME];
    [backButton setExclusiveTouch:YES];
    [backButton setImage:[UIImage imageNamed:BACK_BUTTON_DEFAULT_ICON] forState:UIControlStateNormal];

    UIBarButtonItem *backButtonHolder = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    return backButtonHolder;
}

- (void)viewWillAppear:(BOOL)animated {

    self.navigationItem.leftBarButtonItem = [self createBackMenuButton];
}

答案 1 :(得分:0)

试试这个

UIImage *backButtonImage = [[UIImage imageNamed:@"backt.clicked@2x~iphone.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 27, 0, 27)];
CGRect frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);

UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setImage:backButtonImage forState:UIControlStateNormal];
[button addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

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

修改

UIImage *backButtonImage = [[UIImage imageNamed:@"backt.clicked@2x~iphone.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 27, 0, 27)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.topItem.title = @"";