简体   繁体   English

UIBarButtonItem删除后退按钮标题 - iOS 11

[英]UIBarButtonItem remove Back Button Title - iOS 11

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

I use above to remove the backButtonTitle prior to iOS 11. But on iOS 11 that is not properly working. 我使用上面的方法来移除iOS 11之前的backButtonTitle但是在iOS 11上没有正常工作。 Arrow shifts bit downwards. 箭头向下移动。 在此输入图像描述 How to fix this? 如何解决这个问题?

Edit: Other way by erasing Title can solve my problem but my concern is why that old way is not working anymore. 编辑:通过删除标题的其他方式可以解决我的问题,但我担心的是为什么旧的方式不再工作。

The other way is to set UINavigationControllerDelegate for your navigationController and erase the title in its function. 另一种方法是为您的navigationController设置UINavigationControllerDelegate并删除其函数中的标题。

class NoBackButtonTitleNavigationDelegate: UINavigationControllerDelegate {

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

Use this code, 使用此代码,

var newBackButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.back))
navigationItem?.leftBarButtonItem = newBackButton 

Create Navigation controller class as below. 创建导航控制器类,如下所示。 Assign this "CustomNavViewController" to UINavigationController in your StoryBoard 将此“CustomNavViewController”分配给StoryBoard中的UINavigationController

class CustomNavViewController: UINavigationController,UINavigationControllerDelegate 
{


override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


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

} }

So, no need to do it in every viewControllers. 因此,无需在每个viewControllers中执行此操作。 At last remove below line from AppDelegate class if present, 最后从AppDelegate类中删除以下行,如果存在,

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

Instead of moving title vertically you can move horizontally. 而不是垂直移动标题,您可以水平移动。 Also in case the title is long you can make its color transparent. 如果标题很长,你可以使它的颜色透明。 Works just fine me: 工作得很好我:

let barButtonItemAppearance = UIBarButtonItem.appearance()
    barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
    barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)

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

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