现在我有两个视图控制器。我的问题是,在转换到第二个视图控制器后,我不知道如何隐藏后退按钮。我发现的大多数参考都在Objective-C中。如何在Swift中编写代码?
在Objective-C中隐藏后退按钮代码
[self.navigationItem setHidesBackButton:YES animated:YES];
现在我有两个视图控制器。我的问题是,在转换到第二个视图控制器后,我不知道如何隐藏后退按钮。我发现的大多数参考都在Objective-C中。如何在Swift中编写代码?
在Objective-C中隐藏后退按钮代码
[self.navigationItem setHidesBackButton:YES animated:YES];
Answers:
根据该文件为UINavigationItem
:
self.navigationItem.setHidesBackButton(true, animated: true);
如果您使用的是UITabBarController
:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.hidesBackButton = true
}
迅速
// remove left buttons (in case you added some)
self.navigationItem.leftBarButtonItems = []
// hide the default back buttons
self.navigationItem.hidesBackButton = true
self.navigationItem.hidesBackButton = true
其工作按预期进行。
您可以尝试以下代码
override func viewDidAppear(_ animated: Bool) {
self.navigationController?.isNavigationBarHidden = true
}
这是答案的一个版本
// MARK: - Hiding Back Button
extension UINavigationItem {
/// A Boolean value that determines whether the back button is hidden.
///
/// When set to `true`, the back button is hidden when this navigation item
/// is the top item. This is true regardless of the value in the
/// `leftItemsSupplementBackButton` property. When set to `false`, the back button
/// is shown if it is still present. (It can be replaced by values in either
/// the `leftBarButtonItem` or `leftBarButtonItems` properties.) The default value is `false`.
@IBInspectable var hideBackButton: Bool {
get { hidesBackButton }
set { hidesBackButton = newValue }
}
}
视图控制器的每个导航项将在属性检查器的顶部具有此新属性。