开发者

Adding Custom Back Button on NavigationBar UIButton buttonWithTypw:101 not working

I had used the below mention code for a custom left pointed back button and it was working properly.. but after a few days, it changed back to a rectangular button.. can anyone help me to get it back to its original type.... Thanks...

开发者_StackOverflow社区UIButton *backButton = [UIButton buttonWithType:101];
[backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"Back" forState:UIControlStateNormal];

UIBarButtonItem* newBackButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
graphSecondViewCtrl.navigationItem.leftBarButtonItem = newBackButton;


Button type 101 is not defined in the SDK, and can stop working at any OS upgrade.

I'd use a buttonWithType:UIButtonTypeCustom and then add an image that gives you the look you need by calling setImage:forState on the custom button object.

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:@"backBarButtonNormal.png"] forState:UIControlStateNormal];
[backButton setImage:[UIImage imageNamed:@"backBarButtonHighlighted.png"] forState:UIControlStateHighlighted];

// Rest of the code is the same.

The real question is why you'd need to recreate the standard button that the navigation controller automatically gives you in the first place. Make sure you work with the tools, and not against them.

If you need to track when the view controller gets swapped in and out, you can set your view controller to be the delegate of the navigation controller like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self navigationController] setDelegate:self];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {

    if (viewController != self)
        NSLog(@"We're going away...");
}

- (void)viewDidUnload {
    [super viewDidUnload];
    [[self navigationController] setDelegate:nil];
}


The button type 101 is undocumented. It is not something Apple has agreed to maintain so it can change any time. It is incorrect to rely on such stuff. You should consider a custom button with images to be sure that it will not break like it has for you now.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜