UINavigationBar隐藏按钮文本

时间:2014-05-25 08:58:35

标签: ios uinavigationcontroller

如何从UINavigationController隐藏后退按钮文本? 我只会有"<"而不是"<返回"

34 个答案:

答案 0 :(得分:93)

在界面构建器中,您可以选择上一个控制器的导航项,并将Back Button字符串更改为您希望显示后退按钮的字符串。 如果你想让它空白,例如,只需放一个空格。

也可以使用以下代码更改

[self.navigationItem.backBarButtonItem setTitle:@"Title here"];

Swift

self.navigationItem.backBarButtonItem?.title = ""

答案 1 :(得分:78)

你也可以通过故事板来做到这一点。在上一个控制器的导航项的属性检查器中,您可以设置" "在后退按钮字段中。请参阅下面的图片在此替换"您的标题"到" &#34 ;.通过这样做,您将获得所需的结果。你不需要搞乱这个标题'了。

以编程方式,您可以使用

{{1}}

其中 self 是指推动所需视图控制器的控制器。

Xcode view controller navigation item

示例之前,导航栏之后

<强>之前 Before

<强>后 After

答案 2 :(得分:60)

您可以像这样实施UINavigationControllerDelegate

较旧的斯威夫特

func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
    let item = UIBarButtonItem(title: " ", style: .Plain, target: nil, action: nil)
    viewController.navigationItem.backBarButtonItem = item
}

Swift 4.x

class MyNavigationController: UINavigationController, UINavigationControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
    }

    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        let item = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
        viewController.navigationItem.backBarButtonItem = item
    }
}

backBarButtonItem默认为nil,它会影响下一个推送控制器,因此您只需为所有控制器设置

答案 3 :(得分:37)

将后退按钮的标题设置为@""nil无法正常工作。您需要将整个按钮设置为空(没有标题或图像):

目标C

[self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil]];

夫特

self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)

这应该在导航堆栈中视图控制器顶部的视图控制器上完成(即通过pushViewController方法导航到VC的位置)

答案 4 :(得分:29)

对于有大量视图控制器的情况,此问题的另一个解决方案是使用UIAppearance代理有效地隐藏后退按钮标题文本,如下所示:

UIBarButtonItem *navBarButtonAppearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];

[navBarButtonAppearance setTitleTextAttributes:@{
    NSFontAttributeName:            [UIFont systemFontOfSize:0.1],
    NSForegroundColorAttributeName: [UIColor clearColor] }
                                      forState:UIControlStateNormal];

此解决方案将文本渲染为一个小亮点,类似于手动将后退按钮标题设置为@" ",但它会影响所有导航栏按钮。

我不建议将此作为该问题的一般解决方案,因为它会影响所有导航栏按钮。它会翻转范例,以便您选择何时显示按钮标题,而不是何时隐藏标题。

要选择何时显示标题,可以根据需要手动恢复标题文本属性,或者创建一个UIBarButtonItem的专用子类,它可以执行相同的操作(可能与另一个UIAppearance代理)。

如果您的应用程序需要隐藏大部分后退按钮标题,并且只有少数(或没有)导航按钮是带标题的系统按钮,这可能适合您!

(注意:即使文本颜色清晰,也需要更改字体大小,以确保长标题不会导致中心导航栏标题转换)

答案 5 :(得分:28)

在viewDidLoad或loadView

中添加以下代码
self.navigationController.navigationBar.topItem.title = @"";  

我使用iOS 9在iPhone和iPad上进行了测试

答案 6 :(得分:14)

您可以添加此Objective-C类别,以使导航控制器创建的所有“后退”按钮都没有文本。我刚把它添加到我的AppDelegate.m文件中。

@implementation UINavigationItem (Customization)

/**
 Removes text from all default back buttons so only the arrow or custom image shows up.
 */
-(UIBarButtonItem *)backBarButtonItem
{
    return [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
}

@end

PS - (我不知道如何使这个扩展与Swift一起使用,它有很奇怪的错误。编辑欢迎添加一个Swift版本)

答案 7 :(得分:10)

以编程方式从后退按钮中删除文本,在代码下面使用,这将从xcode7及以上版本开始。

self.navigationController.navigationBar.topItem.title = @“”;

manualLy在故事板中,选择视图控制器上的导航栏,并在后面的按钮文本中添加“”。

这会奏效。谢谢

答案 8 :(得分:9)

唯一没有副作用的是创建自定义后退按钮。只要您不提供自定义操作,即使幻灯片手势也可以。

extension UIViewController {
func setupBackButton() {
    let customBackButton = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    navigationItem.backBarButtonItem = customBackButton
}}

不幸的是,如果您希望所有后退按钮都没有任何标题,则需要在所有视图控制器中设置此自定义后退按钮:/

override func viewDidLoad() {
    super.viewDidLoad()

    setupBackButton()
}

将空格设置为标题而不是空字符串非常重要。

答案 9 :(得分:7)

目前的答案并不奏效。我想完全删除标题,但文字&#34;返回&#34;没有消失。

返回上一个视图控制器并设置其title属性:

self.title = @" ";

ONLY 在上一个View Controller没有标题时有效

答案 10 :(得分:4)

我在上面做了一些尝试,但是没有用。这对我有用:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.topItem?.title = ""
}

答案 11 :(得分:4)

将前一个VC的标题设置为“”带空格的字符串。带后退按钮的标题将替换为单个空格字符串。

Self.title = " "

On Back press再次将视图重置为viewWillAppear中的原始标题。

答案 12 :(得分:4)

替代方式 - 使用自定义NavigationBar类。

sudo npm install

也就是说,这将删除整个项目。 只需为UINavigationController设置自定义类。

答案 13 :(得分:3)

使用覆盖public static BigInteger Fib(int A, int B, int n) { if (n <= 0) return 0; n = n - 1; _auxOne = 0; _auxTwo = 1; Matrix[0, 0] = _auxTwo; //a Matrix[0, 1] = _auxOne; //b Matrix[1, 0] = _auxOne; //c Matrix[1, 1] = _auxTwo + _auxOne; //d while (n > 0) { if (n % 2 != 0) { _auxOne = Matrix[1, 1] * Matrix[0, 1] + Matrix[1, 0] * Matrix[0, 0]; //(db+ca) _auxTwo = Matrix[1, 1] * (Matrix[0, 1] + Matrix[0, 0]) + Matrix[1, 0] * Matrix[0, 1]; //(d(b+a)+cb) Matrix[0, 0] = _auxOne; Matrix[0, 1] = _auxTwo; } _auxOne = BigInteger.Pow(Matrix[1, 0], 2) + BigInteger.Pow(Matrix[1, 1], 2); //(c²+d²) _auxTwo = Matrix[1, 1] * (2 * Matrix[1, 0] + Matrix[1, 1]); //(d*(2c+d)) Matrix[1, 0] = _auxOne; Matrix[1, 1] = _auxTwo; n = n / 2; } return Matrix[0, 0] + Matrix[0, 1]; }

的自定义NavigationController
pushViewController

答案 14 :(得分:3)

已经有很多答案了,这是我关于这个问题的两分钱。 我发现这种方法确实很健壮。 您只需要在segue之前将其放入viewController中即可。

快捷键4:

$ volume_id=$( printf '%s\n' foo bar baz | create_volume )
$ echo "$volume_id"
>>> foo,bar,baz

答案 15 :(得分:2)

我在这篇文章中尝试了一切。唯一可行的解​​决方案是@VoidLess&#39>

这是相同的答案,但更完整

class CustomNavigationController: UINavigationController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.delegate = self
    }
}


// MARK:UINavigationControllerDelegate
extension CustomNavigationController {
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
    }
}

答案 16 :(得分:2)

Swift 3.1 您可以通过实现UINavigationController的委托方法来完成此操作。

func navigationController(_ navigationController: UINavigationController, 
                          willShow viewController: UIViewController, animated: Bool) {

    /** It'll hide the Title with back button only,
     ** we'll still get the back arrow image and default functionality.
     */
    let item = UIBarButtonItem(title: " ", style: .plain, target: nil, 
                               action: nil)
    viewController.navigationItem.backBarButtonItem = item
}

答案 17 :(得分:2)

在Swift3中,

如果设置全局设置

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    // ..

    let BarButtonItemAppearance = UIBarButtonItem.appearance()
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .highlighted)


    // ...

}

答案 18 :(得分:2)

Swift 5

viewController.navigationItem.backButtonDisplayMode = .minimal

答案 19 :(得分:1)

这是我对iOS11的解析,我在applicationDidFinishLaunchingWithOptions中改变了UIBarButtonItem的外观:

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-100, 0), for:UIBarMetrics.default)

你不能改变Y偏移,因为它也会在iOS11中改变后退按钮的位置,但在iOS10及以下版本中它是正常的。

答案 20 :(得分:1)

对于那些想要全局隐藏按钮标题的人。

你可以像viewDidLoad那样混淆UIViewController

+ (void)overrideBackButtonTitle {

    NSError *error;

    // I use `Aspects` for easier swizzling.

    [UIViewController aspect_hookSelector:@selector(viewDidLoad)
                              withOptions:AspectPositionBefore
                               usingBlock:^(id<AspectInfo> aspectInfo)
    {

        UIViewController *vc = (UIViewController *)aspectInfo.instance;

        // Check whether this class is my app's view controller or not.
        // We don't want to override this for Apple's view controllers,
        // or view controllers from external framework.

        NSString *className = NSStringFromClass([vc class]);
        Class class = [NSBundle.mainBundle classNamed:className];
        if (!class) {
           return;
        }

        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStylePlain target:nil action:nil];
        vc.navigationItem.backBarButtonItem = backButton;

    } error:&error];

    if (error) {
        NSLog(@"%s error: %@", __FUNCTION__, error.localizedDescription);
    }

}

用法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[self class] overrideBackButtonTitle];
    return YES;
}

答案 21 :(得分:0)

这里的大多数解决方案的问题是,在后退项目上设置空文本不适用于长按后退按钮的新功能。而不是显示正确的标题,它只是显示一个空列表

Empty list on long pressing a back button

相反,您可以使用iOS14 的新按钮显示模式或依赖iOS13 的新外观 API,将文本设置为大小 0< /强>。请注意,有些人正在使用颜色(清晰),这也不能很好地工作,因为它使用了空间并将其从您的标题中删除。如果标题够长,你会看到它被剪掉了

结果代码:

public extension UINavigationBar {
    func fixAppearance() {
        if #available(iOS 14.0, *) {
            topItem?.backButtonDisplayMode = .minimal
        } else if #available(iOS 13.0, *) {
            let newAppearance = standardAppearance.copy()
            newAppearance.backButtonAppearance.normal.titleTextAttributes = [
                .foregroundColor: UIColor.clear,
                .font: UIFont.systemFont(ofSize: 0)
            ]
            standardAppearance = newAppearance
        } else {
            topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        }
    }
}

然后您只需要在呈现视图控制器时调用该方法,因此您可以从基类(例如在 viewWillAppear 上)调用它,或者像在其他答案中一样向导航控制器添加一些委托这个帖子。

答案 22 :(得分:0)

在iOS 11中,我们发现将UIBarButtonItem外观的文本字体/颜色设置为非常小的值或清晰的颜色将导致其他条形项消失(系统不遵守UIBarButton项的类再将它转换为_UIModernBarButton)。同时将背景文本的偏移设置为屏幕外将导致交互式弹出时闪烁。

所以我们调来addSubView

+ (void)load {
    if (@available(iOS 11, *)) {
        [NSClassFromString(@"_UIBackButtonContainerView")     jr_swizzleMethod:@selector(addSubview:) withMethod:@selector(MyiOS11BackButtonNoTextTrick_addSubview:) error:nil];
    }
}

- (void)MyiOS11BackButtonNoTextTrick_addSubview:(UIView *)view {
    view.alpha = 0;
    if ([view isKindOfClass:[UIButton class]]) {
        UIButton *button = (id)view;
        [button setTitle:@" " forState:UIControlStateNormal];
    }
    [self MyiOS11BackButtonNoTextTrick_addSubview:view];
}

答案 23 :(得分:0)

UINavigationControllerDelegate navigationController(_, willShow:, animated:)方法实现帮了我大忙。

此处是完整视图控制器源代码。如果您想在整个应用程序中应用此功能,请使所有视图控制器都源自BaseViewController

class BaseViewController: UIViewController {
    // Controller Actions
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        updateNavigationBar()
    }
    
    //This is for custom back button image.
    func updateNavigationBar() {
        let imgBack = UIImage(named: "icon_back")
        self.navigationController?.navigationBar.backIndicatorImage = imgBack
        self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = imgBack
        self.navigationItem.backBarButtonItem = UIBarButtonItem()
    }
}

extension BaseViewController: UINavigationControllerDelegate {
    //This is to remove the "Back" text from back button.
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        viewController.navigationItem.backBarButtonItem = UIBarButtonItem()
    }
}

答案 24 :(得分:0)

XCode 11.5 Swift 5

一种非常简单的方法-尽管可能有些怪异- 以编程方式,如果不需要自定义后退按钮,则是在要推入堆栈的视图控制器中将字体大小设置为零,并从viewDidLoad调用类似的内容

private func setupNavBar() {
    let appearance = UINavigationBarAppearance()
    appearance.configureWithDefaultBackground()
    
    let backButtonAppearance = UIBarButtonItemAppearance()
    backButtonAppearance.normal.titleTextAttributes = [.font: UIFont(name: "Arial", size: 0)!]
    appearance.backButtonAppearance = backButtonAppearance

    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    navigationItem.compactAppearance = appearance
}

答案 25 :(得分:0)

我的解决方案: -XCode:10.2.1 -斯威夫特:5

  • 父视图控制器:
    • self.title =“”
  • 子视图控制器:
    • self.navigationItem.title =“您的标题” //为视图控制器设置标题

答案 26 :(得分:0)

背景文本来自上一个View Controller的navigationItem.titlenavigationItem.titleself.title自动设置。解决问题的简便方法是钩setTitle:,确保navigationItem.title = @""

将此代码放在AppDelegate.m上就可以了。

    [UIViewController aspect_hookSelector:@selector(setTitle:)
                              withOptions:AspectPositionAfter
                               usingBlock:^(id<AspectInfo> aspectInfo, NSString *title) {
        UIViewController *vc = aspectInfo.instance;
        vc.navigationItem.titleView = ({
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
            titleLabel.text = title;
            titleLabel;
        });
        vc.navigationItem.title = @"";
    } error:NULL];

有关更多详细信息,请访问https://www.jianshu.com/p/071bc50f1475(简单的中文酶)

答案 27 :(得分:0)

@page "/somepage"

@dynamicComponent

@functions{
 BlazorComponent dynamicComponent = Activator.CreateInstance<Components.MyComponent>();
}

答案 28 :(得分:0)

我正在努力解决这个问题,因为我有一个自定义导航控制器。 我能够在自定义导航控制器类中使用此代码删除所有视图控制器中的后退项目文本 override func viewDidLayoutSubviews() { self.navigationBar.backItem?.title = "" }

这将使用此自定义导航控制器删除所有后项目标题。

答案 29 :(得分:0)

终于找到了完整的解决方案来隐藏整个应用程序中的默认背文本。

只需添加一个透明图像,然后在AppDelegate中添加以下代码。

UIBarButtonItem.appearance().setBackButtonBackgroundImage(#imageLiteral(resourceName: "transparent"), for: .normal, barMetrics: .default)

答案 30 :(得分:-1)

如果您要定位 iOS 13 ,并且以后可以使用this new API 全局隐藏后退按钮标题

let backButtonAppearance = UIBarButtonItemAppearance()
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]

UINavigationBar.appearance().standardAppearance.backButtonAppearance = backButtonAppearance
UINavigationBar.appearance().compactAppearance.backButtonAppearance = backButtonAppearance
UINavigationBar.appearance().scrollEdgeAppearance.backButtonAppearance = backButtonAppearance

答案 31 :(得分:-2)

以下方法适用于iOS 11,并且可以安全地在其他iOS版本上崩溃。 这样做可能会在App Store审核中拒绝您的应用,因为UIModernBarButton和UIBackButtonContainerView都是私有API。 放在AppDelegate。

    if
        let UIModernBarButton = NSClassFromString("_UIModernBarButton") as? UIButton.Type,
        let UIBackButtonContainerView = NSClassFromString("_UIBackButtonContainerView") as? UIView.Type {

        let backButton = UIModernBarButton.appearance(whenContainedInInstancesOf: [UIBackButtonContainerView.self])
        backButton.setTitleColor(.clear, for: .normal)
    }

答案 32 :(得分:-5)

Swift版本,在全球范围内运作良好:

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Normal)
        UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.clearColor()], forState: UIControlState.Highlighted)

        return true
    }

答案 33 :(得分:-5)

这是来自我的xamarin.forms代码,该类派生自NavigationRenderer

NavigationBar.Items.FirstOrDefault().BackBarButtonItem = new UIBarButtonItem( "", UIBarButtonItemStyle.Plain, null);