开发者

Navigation Back Button does not show,how to add back button to UINavigationItem

I add UINavigationBar via Library to view. I also add UINavigationItem to this NavigationBar.

In viewController.m,I adde开发者_运维知识库d the following code but back button doesn't show.

self.navigationItem.title = @"List";
self.navigationItem.backBarButtonItem.title = @"Back";


Try this:

self.navigationItem.leftItemsSupplementBackButton = YES;

or

navigationItem.leftItemsSupplementBackButton = true

If you customize your navigation controller with UIBarButtonItems the framework removes the back button by default, since it assumes you are customizing the Navigation bar. Use that line to add the back button in again.


self.navigationItem.backBarButtonItem will be shown only after your have pushed another one view to navigation stack, controlled by self.navigationController, if no left button on navigation bar is displayed.

From Apple docs:

When this item is the back item of the navigation bar—when it is the next item below the top item—it may be represented as a back button on the navigation bar. Use this property to specify the back button. The target and action of the back bar button item you set should be nil. The default value is a bar button item displaying the navigation item’s title.


Try this code,

**To hide your default back button use this,**



self.navigationItem.hidesBackButton = YES;

UIImage* image = [UIImage imageNamed:@"back.png"];
    CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
    UIButton* backbtn = [[UIButton alloc] initWithFrame:frame];
    [backbtn setBackgroundImage:image forState:UIControlStateNormal];
    [backbtn setShowsTouchWhenHighlighted:YES];
    [backbtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];
    [self.navigationItem setLeftBarButtonItem:backButtonItem];
    [backButtonItem release];
    [backbtn release];

Action Event for back button:

-(IBAction)goBack{
//ur code here

}


Try this code may be it's help to u

UIButton *moreButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreButton1 setImage:[UIImage imageNamed:@"left_arrow.png"] forState:UIControlStateNormal];
    //[moreButton setImage:[UIImage imageNamed:@"Button_OtherInfo_Active.png"] forState:UIControlStateHighlighted];
    [moreButton1 addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [moreButton1 setFrame:CGRectMake(0, 0, 50,30)];
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:moreButton1];

put this code in viewDidLoad

-(void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];  
}


This is a bit old, but in case someone is looking for an answer...

The back button shows up automatically, but the current view (not the one being pushed) needs to have a title. eg:

- (void)viewDidLoad
{
    self.title = @"Home";
    [super viewDidLoad];
}

Then, when you push a view onto the view stack with

[self.navigationController pushViewController:aViewController animated:YES];

the back button shows up. No need to mess with UINavigationItem or UINavigationBar. Use those to customize the navigation bar. Take a look at the example project called NavBar, part of xcode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜