小编典典

如何删除所有导航栏后退按钮标题

swift

当我按下a时UIViewController,它在new的后退按钮中有一些标题UIViewController,如果标题中包含很多文本,则在iPhone
4s中看起来不太好,所以我想删除它。

如果我在prepareForSegue函数中添加一些代码,那将是一个麻烦。

还有更好的方法吗?


阅读 347

收藏
2020-07-07

共1个答案

小编典典

如果要返回箭头,则将以下代码放入AppDelegate文件中didFinishLaunchingWithOptions

For Objective-C

 [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

For Swift

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

另一个选择在下面给出。

Objective C

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

Swift

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

更新:

    let BarButtonItemAppearance = UIBarButtonItem.appearance()

    let attributes: [NSAttributedStringKey: Any] = [
    BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
            NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
            NSAttributedStringKey.foregroundColor: UIColor.clear]

    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

更新SWIFT 4.1:

    let attributes = [NSAttributedStringKey.font:  UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]

    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
    BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)

使用偏移

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

因此可能是您的问题已解决。

快乐的编码。

2020-07-07