Create UIBarButtonItem with back button style

别来无恙 提交于 2020-01-04 04:00:32

问题


I'm looking for a way to create programmatically an UIBarButtonItem that looks like a back button of UINavigationBar. Apparently seems like that the back button appears only after a push on the UINavigationController.

So I'm able to insert only a button with the "cancel" style. But my goal is to create a button with the "New Item" style.

Ideas ?


回答1:


the short answer is you cannot do it.

you can save an image and you can put the image to that position, but the back button is managed by private part of the UINavigationBar.




回答2:


I think you need to use image. and then set the image be the buttons backgroundImage. such as :

navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:@"Detail"] autorelease];

        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];

    [button setImage:[UIImage imageNamed:@"plain.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClicked:)
        forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
                               initWithCustomView:button];

    navigationItem.leftBarButtonItem = buttonItem;

    [buttonItem release];
    [button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];



回答3:


Take a look on this answer : Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar

It gives you a psd file with image that you need.




回答4:


It works for me: [self.navigationItem.leftBarButtonItem setTitle:@"Back"];

(custom text with back arrow)



来源:https://stackoverflow.com/questions/14605425/create-uibarbuttonitem-with-back-button-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!