简体   繁体   English

带有自定义后退按钮的UINavigationController子类

[英]UINavigationController subclass with custom back button

I know I can set custom back button from the view controller itself, something like: 我知道我可以从视图控制器本身设置自定义后退按钮,例如:

- (void)setBackButton
{
    UINavigationBar* navBar = self.navigationController.navigationBar;
    UIButton* backButton = [navBar backButtonWith:[UIImage imageNamed:@"navigationBarBackButton.png"] 
                                        highlight:nil 
                                     leftCapWidth:14.0];

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

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

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

The problem is that I need to do it for all my view controllers... 问题是我需要为所有视图控制器执行此操作...
One solution is to put this code in some BasicViewController and all my view controllers will subclass it. 一种解决方案是将此代码放在一些BasicViewController中,我的所有视图控制器都将它子类化。

But my question is can I subclass the UINavigationCotroller itself and set it's nab bar left button to this custom button? 但我的问题是我可以将UINavigationCotroller本身子类化并将它的nab bar左键设置为此自定义按钮吗?

The right way to do this is using UIAppearance 正确的方法是使用UIAppearance

It provides methods on UIBarButtonItem such as 它提供了UIBarButtonItem方法,例如

- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics

and

- (void)setBackButtonBackgroundVerticalPositionAdjustment:(CGFloat)adjustment forBarMetrics:(UIBarMetrics)barMetrics

不。最好有一个自定义的UIViewController来处理这种行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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