我想从后退按钮中删除文本,但我想保留图标。我试过了
let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
但是,这会完全删除文本和图标。
答案 0 :(得分:101)
我知道这已经有了答案,但您也可以在代码中执行此操作(如果您正在使用nib)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
在第一个视图控制器中添加以上内容。
请注意,您需要为每个正在推送的视图控制器执行此操作。因此,如果您有3个视图控制器,并且想要从所有视图控制器中删除背面文本,则必须在视图控制器1和2中添加该行。
答案 1 :(得分:75)
@ rmd2的方法几乎是正确的,但您应该选择后退按钮指向的控制器的导航栏,并在后退按钮字段中键入" "
。
答案 2 :(得分:27)
如果你想从推送的视图控制器中删除后退按钮的标题,让我们在subSettingsViewController中说<Settings
到<
然后你要设置 backBarButtonItem 标题在SettingsViewController的 viewWillDisappear()方法中。
目标-C:
- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:self.navigationItem.backBarButtonItem.style target:nil action:nil];
}
夫特:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
答案 3 :(得分:25)
后退按钮的文本取决于主视图的标题。
诀窍是如果主视图消失则清除标题,如果再次显示则再次设置:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
// needed to clear the text in the back navigation:
self.navigationItem.title = " "
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "My Title"
}
答案 4 :(得分:21)
如果你想要后退箭头,那么将代码放入AppDelegate文件到didFinishLaunchingWithOptions方法中。
对于Swift
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
答案 5 :(得分:20)
在我的情况下,对于自定义图标和标题,这就是诀窍(Swift 4)
let imgBack = UIImage(named: "ic_back")
navigationController?.navigationBar.backIndicatorImage = imgBack
navigationController?.navigationBar.backIndicatorTransitionMaskImage = imgBack
navigationItem.leftItemsSupplementBackButton = true
navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
答案 6 :(得分:19)
经过大量搜索后,我发现了最佳,简单的解决方案,这将影响所有用Swift 4.2编写的viewControllers
var showChar = 100;
var ellipsestext = "...";
var moretext = "Ещё";
var lesstext = "Скрыть";
$('.more').each(function () {
var content = $(this).html();
if (content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length - showChar);
var position = h.indexOf('</li>');
var h = [h.slice(0, position), '</span></li><span class="morecontent">', h.slice(position + 5)].join('');
var html = c + '<span class="moreellipses">' + ellipsestext + '</span><span class="morecontent"><span>'
+ h + '</span><a href="" class="morelink">' + moretext + '</a></span>';
$(this).html(html);
}
});
$(".morelink").click(function () {
if ($(this).hasClass("less")) {
$(this).removeClass("less");
$(this).html(moretext);
$('.morecontent').each(function() {
$(this).css('display', 'none');
});
$('.moreellipses').css('display', 'inline');
} else {
$(this).addClass("less");
$(this).html(lesstext);
$('.morecontent').each(function () {
$(this).css('display', 'inline');
});
$('.moreellipses').css('display', 'none');
}
return false;
});
答案 7 :(得分:13)
答案 8 :(得分:11)
终于找到了完美的解决方案。
只需添加一个透明图像,然后在AppDelegate中添加以下代码。
UIBarButtonItem.appearance().setBackButtonBackgroundImage(#imageLiteral(resourceName: "transparent"), for: .normal, barMetrics: .default)
答案 9 :(得分:10)
对于 Swift 4 + ,将这些行放在AppDelegate
didFinishLaunchingWithOptions
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
BarButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)
答案 10 :(得分:8)
快速4
self.navigationController?.navigationBar.topItem?.title = ""
答案 11 :(得分:4)
如果您有ViewControllerA,并且想要导航到ViewControllerB,则在ViewControllerA中,您应该将当前navigationItem设置为新的UIBarButtonItem,并将标题设置为带有空格的字符串,然后再推送到另一个视图控制器:
将标题设置为带空格的字符串,您无法设置nil或&#34;&#34; (空字符串)因为默认值为nil
let backItem = UIBarButtonItem()
backItem.title = " "
navigationItem.backBarButtonItem = backItem
let controllerB = ViewControllerB()
navigationController?.pushViewController(controllerB, animated: true)
答案 12 :(得分:3)
您可以使用UINavigationController
的委托方法从后退按钮中删除文本。
class CustomNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
}
extension CustomNavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: String(), style: .plain, target: nil, action: nil)
}
}
答案 13 :(得分:3)
let button: UIButton = UIButton (type: UIButtonType.Custom)
button.setImage(UIImage(named: "imageName"), forState: UIControlState.Normal)
button.addTarget(self, action: "backButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
button.frame = CGRectMake(0, 0, 30, 30)
let barButton = UIBarButtonItem(customView: button)
self.navigationItem.leftBarButtonItem = barButton
func backButtonPressed(btn : UIButton) {
// Your code
}
答案 14 :(得分:3)
将此代码放入每个VC中,这会推送另一个代码
select distinct t.id
,a.nome
,a.cognome
,t.operazione
from ticket_notifice t
,anagrafica a
,amb_impegnative c
,amb_metadata d
where t.useridrichiedente = useridrichiedentep
and t.stato = '1'
and t.notification = '0'
and regexp_substr(t.nosologico, '[^AMB_]+$', 1, 1) = c.id_amb_metadati
and c.id_amb_metadati = d.id_amb_metadati
and d.idassistito = a.idassistito
union
select distinct t.id
,a.nome
,a.cognome
,t.operazione
from ticket_notifice t
,ricovero r
,anagrafica a
where t.useridrichiedente = useridrichiedentep
and t.stato = '1'
and t.notification = '0'
and t.nosologico = r.nosologico
and r.idanagrafica = a.id
union
select distinct t.id
,a.nome
,a.cognome
,t.operazione
from ticket_notifice t
,anagrafica a
,ps_metadata m
where t.useridrichiedente = useridrichiedentep
and t.stato = '1'
and t.notification = '0'
and regexp_substr(t.nosologico, '[^PS_]+$', 1, 1) = m.episodiopsnum
and m.idanagrafica = a.id
答案 15 :(得分:2)
要从导航控制器堆栈中的所有viewcontrollers中删除:
子类UINavigationController并添加:
override func show(_ vc: UIViewController, sender: Any?) {
setEmptyBackButton(vc)
super.show(vc, sender: sender)
}
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
setEmptyBackButton(viewController)
super.pushViewController(viewController, animated: animated)
}
func setEmptyBackButton(_ viewController: UIViewController) {
viewController.navigationItem.backBarButtonItem =
UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
答案 16 :(得分:2)
有时,如果标题很长,它不会仅改变标题颜色。因为它可能会将导航栏标题向左移动。因此,为了防止它,您可能需要水平移动条形按钮标题,并使其透明:
let barButtonItemAppearance = UIBarButtonItem.appearance()
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
barButtonItemAppearance.setBackButtonTitlePositionAdjustment(UIOffsetMake(-200, 0), for:UIBarMetrics.default)
答案 17 :(得分:1)
以编程方式执行此操作的最简单方法是从父视图控制器(调用push的控制器)中设置backBarButtonItem
。
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backBarButtonItem
}
}
此处有更多详细信息,https://sarunw.com/posts/how-to-remove-text-from-uinavigationbar-back-button/
答案 18 :(得分:1)
对我来说,覆盖所有ViewControllers
的另一种方法是扩展UINavigationController
并设置backBarButtonItem
的{{1}}。
topViewController
在Xcode 11.2.1上:
Swift 5
答案 19 :(得分:1)
答案 20 :(得分:1)
Swift 5 版本移除返回按钮标题并设置自定义图像 ios 13.0+
let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backButtonAppearance = backButtonAppearance
navigationBarAppearance.setBackIndicatorImage(#imageLiteral(resourceName: "back"), transitionMaskImage: #imageLiteral(resourceName: "back"))
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
在App delegate didFinishLaucnh中添加这段代码
答案 21 :(得分:1)
您应该选择后退按钮指向的控制器FROM的导航栏并输入&#34; &#34;在后退按钮字段中。
例如,如果您将A控制器推送到B控制器,请在控制器导航栏中输入空格。
答案 22 :(得分:1)
对我来说,这就是诀窍:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
self.navigationItem.title = " "
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationItem.title = "my amazing title"
navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}
请注意,如果您只设置标题而不修改 backBarButtonItem,它将 显示 才能工作。但是如果您尝试使用手势而不是取消并继续使用推送的视图控制器,则后面的标题将会返回。
使用 Swift 4
答案 23 :(得分:0)
对于我们无法完全控制所有以前的视图控制器的情况(例如,如果我们在框架中工作),我们可以按以下方式删除后退按钮的标题:
// For iOS 10
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = String()
// For iOS 11
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = nil
它的作用是导航到导航堆栈的最后一项并删除其后标题。当我们的视图控制器出现时,请确保保存原始图片:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
originalBackButtonTitle = navigationController?.navigationBar.items?.last?.backBarButtonItem?.title
// Delete title somewhere here...
}
然后重新分配它,以不破坏应用程序的任何部分:
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.items?.last?.backBarButtonItem?.title = originalBackButtonTitle
super.viewWillDisappear(animated)
}
答案 24 :(得分:0)
这将解决您的问题:
import UIKit
extension UINavigationController{
func customizeBackArrow(){
let yourBackImage = UIImage(named: "icon_back_arrow")
self.navigationBar.backIndicatorImage = yourBackImage
self.navigationBar.tintColor = Common.offBlackColor
self.navigationBar.backIndicatorTransitionMaskImage = yourBackImage
navigationItem.leftItemsSupplementBackButton = true
self.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "",
style: .plain, target: self, action: nil)
}
}
答案 25 :(得分:0)
我尝试了几个答案,但我无法让它们在所有情况下都能正常工作。因此,这是一种不影响导航栏标题(如果已设置)的解决方法。
guard let items = viewController.navigationController?.navigationBar.items else { return }
for item in items {
if item.title == nil {
item.title = ""
}
}
答案 26 :(得分:0)
1。创建UINavigationController的自定义类
import UIKit
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}
extension NavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController.navigationItemBackButtonTextIsHidden {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
}
2。添加UIViewController扩展
import UIKit
extension UIViewController {
@objc var navigationItemBackButtonTextIsHidden: Bool { return false }
}
3。更新您的ViewController类
import UIKit
class ViewController: UIViewController {
override var navigationItemBackButtonTextIsHidden: Bool { return true }
}
import UIKit
// MARK: - ViewController
class ViewController: UIViewController {
var screenCounter = 1
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationItem()
}
override var navigationItemBackButtonTextIsHidden: Bool { return (screenCounter % 2) == 0 }
}
extension ViewController {
private func setupNavigationItem() {
navigationItem.title = "VC \(screenCounter)"
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "push", style: .plain, target: self, action: #selector(pushBarButtonTouchedUpInside))
}
@objc func pushBarButtonTouchedUpInside(button: UIBarButtonItem) {
guard let navigationController = navigationController else { return }
let viewController = ViewController()
viewController.screenCounter = screenCounter + 1
viewController.view.backgroundColor = .white
navigationController.pushViewController(viewController, animated: true)
}
}
// MARK: - NavigationController
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}
}
extension NavigationController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController.navigationItemBackButtonTextIsHidden {
viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
}
// MARK: - UIViewController extension
extension UIViewController {
@objc var navigationItemBackButtonTextIsHidden: Bool { return false }
}
答案 27 :(得分:0)
这对我有用
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.topItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
答案 28 :(得分:0)
在带有Swift的Xcode 9.2中,它的工作方式如下:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
答案 29 :(得分:0)
一种简单的编程方法,没有副作用,它是使用源代码的 awakeFromNib 方法中的空项目初始化 navigationItem.backBarButtonItem 。 strong>控制器(您要从中导航的控制器):
override func awakeFromNib() {
super.awakeFromNib()
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
注意:如果稍后像 viewDidLoad()方法中那样初始化后退按钮,将比丢失后退滑动功能(从左侧边缘滑动到右侧将使您在导航堆栈中退后一步。
然后,如果您想为不同的目标控制器使用不同的后退按钮文本,并且使用的是segues,则可以在 prepare(for segue :, sender:)方法中设置标题,例如这个:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let item = navigationItem.backBarButtonItem {
switch segue.identifier {
case "SceneOne": item.title = "Back"; break
case "SceneTwo": item.title = "Home"; break
case "SceneThree": item.title = nil; break // Use this scene's title
default: item.title = "" // No text
}
}
}
答案 30 :(得分:0)
我在Appdelegate的didFinishLaunchingWithOptions函数中使用了此代码。
model = BHA_overall
答案 31 :(得分:-3)
Swift 5,Xcode 10.2
我尝试了所有方法(扩展导航控制器等)..但最终发现问题出在推视图控制器上
之前:
self.navigationController?.pushViewController(vc, animated: false)
解决方案:
self.navigationController?.pushViewController(vc, animated: true)
这对我有用