문제

Here's my code for removing the back UIBarButtonItem of a navigation bar and replacing it with a cancel button:

UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = cancelButtonItem;

Every example I've seen online doesn't hide the backButton before replacing it with a custom item. I may be wrong but it just seems like one unneeded line of code.

도움이 되었습니까?

해결책

Did you try using UIBarButtonSystemItemCancel instead of UIBarButtonItemStylePlain in the style of your UIBarButtonItem?

Try this,

    UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAction)];
    self.navigationItem.leftBarButtonItem = cancelButton;

Also if you have used self.navigationItem.backBarButtonItem in your parentViewController, it will show that button in the childViewController's leftBarButtonItem by default, if your custom leftBarButton in the childViewController is not added properly.

다른 팁

Try this. Write this in your app delegates did finish launching.

LoginViewController *loginVc = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        self.navigationController = [[UINavigationController alloc] initWithRootViewController:loginVc];

    self.window.rootViewController = self.navigationController;
    self.navigationController.navigationBarHidden =YES;

// and use uibutton in your view controller at place of back button or cancel button
// and u can push or pop your view from there.
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top