简体   繁体   中英

Customize back button in IOS

How can I make a customize back button without Text in iOS using barmetrics?

I wanna make something like http://a397.phobos.apple.com/us/r1000/081/Purple/v4/e6/be/2d/e6be2d9e-dc95-7e44-b1ed-9386fa9f4d02/mzl.zwjkpepo.320x480-75.jpg

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

You can place that in your app delegate and it will set the background image to all back buttons in the app (for that control state and bar metrics, of course).

Edit: If you want to something different then use this code:

- (void)setBackButton
{
    UIButton *backButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];[button setFrame:CGRectMake(0, 0, 32, 32)];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
}

- (void)backButtonTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}  

How about Something like this?

UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(YourFrame)];

[backButton setTitle:@"<" forState:UIControlStateNormal];

[backButton addTarget:self action:@selector(callSelectorMethod) forControlEvents:UIControlEventTouchUpInside];

self.navigationItem.rightBarButtonItem  = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];

[backButton release],backButton=nil;

Here is the solution:

 UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    [button setFrame:CGRectMake(0, 0, 32, 32)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

Thanks to Best Coder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM