【问题标题】:Swift strange back button text in iphone plusiphone plus中的Swift奇怪的后退按钮文本
【发布时间】:2017-04-25 08:54:37
【问题描述】:

请看我的截图,iphone plus 上总是显示蓝色的“Back”文字(6s plus,7 plus 模拟器和真机)。它不会在小屏幕 iphone 上显示。我尝试了很多方法来隐藏/从当前/上一个控制器更改它,但没有运气。

那么为什么它可以在较小的 iphone 上运行,而不是在 plus 上呢?

谁能帮帮我:(。谢谢。

代码如下:

@IBAction func filter(_ sender: Any) {
    let view:FilterViewController = self.storyboard?.instantiateViewController(withIdentifier: "FilterViewController") as! FilterViewController
    view.superVC = self
    view.currentFilter = currentFilter
    self.setLeftCloseNavigation()
    self.navigationController?.pushViewController(view, animated: true)
}

func setLeftCloseNavigation(){
    self.navigationController?.navigationBar.backgroundColor = UIColor.clear
    self.navigationController?.navigationBar.isTranslucent = true
    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.layer.mask = nil

    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "icon_close")?.withRenderingMode(.alwaysOriginal)
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "icon_close")?.withRenderingMode(.alwaysOriginal)
    navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}

这是推送控制器中的 viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    statusBar = UIColor.black
    setResetNavigation() }

func setResetNavigation(){
    navigationItem.hidesBackButton = false

    let skipButton = UIButton(frame: CGRect(x: 0, y: 0, width: 70, height: 30))
    skipButton.setTitle("Reset all".localized(), for: .normal)
    skipButton.setTitleColor(UIColor.black, for: .normal)
    skipButton.titleLabel?.font = UIFont(name: "HJGothamMedium", size: 16)
    skipButton.addTarget(self, action: #selector(resetAllClicked), for: .touchUpInside)
    let skip = UIBarButtonItem(customView: skipButton)
    navigationItem.rightBarButtonItem = skip
}

这是视图层次结构

【问题讨论】:

  • 你能显示一些代码吗
  • 在情节提要中检查您的视图并删除该按钮。
  • 这是 presentViewController 吗?
  • 没有@KKRocks,这是推送后的视图控制器
  • @Bhupat 我添加了一些代码

标签: ios iphone swift button navigationbar


【解决方案1】:

添加此功能:

   override func viewDidAppear(_ animated: Bool) {
          setResetNavigation()
          self.navigationController?.navigationBar.backItem?.title = ""
    }

【讨论】:

  • 谢谢,但我不想删除它,只想删除文本
  • 是的,我也试过了,它适用于较小的 iphone,但不适用于 plus :(
  • 请稍等。我在 7 秒内试试这个
  • 是的,我正在使用自动布局,这是 FilterViewController ,是从 tabBarController 的子控制器推送的
  • 你能给我 TeamViewer 吗?
【解决方案2】:

试试这个

 self.navigationItem.hidesBackButton = true

或者检查您的故事板,它将保留

【讨论】:

  • 谢谢,但我不想隐藏,只想删除文字
【解决方案3】:

使用下面的行删除文本

navigationController?.navigationBar.topItem?.title = ""

【讨论】:

  • 谢谢,我试过了,但没用 :(。我把它放在前一个控制器(推送前)和呈现控制器(viewdidload,viewdidappear)中
  • 那个十字架是什么。如果它是一个按钮,看看你是否错误地将名称返回到那个按钮以及一个十字架图像
【解决方案4】:

您可以检查您的 UI 层次结构,如果找到相关视图,则删除该视图:

您还可以通过从调试导航器的进程视图选项菜单中选择 View UI Hierarchy 或通过选择 Debug > View Debugging > Capture View Hierarchy 来调用视图调试器。

【讨论】:

  • 请看一下我刚刚添加的视图层次的截图
【解决方案5】:

要隐藏后面的文本,您需要在推送呈现的视图控制器的视图控制器上将导航项标题设置为空格字符:

self.navigationItem.title = " "

请注意,您必须在前一个视图控制器上设置它,而不是在最上面的视图控制器上。另外你必须设置一个空格字符而不是一个空字符串!!!

您也可以直接在情节提要上执行此操作

【讨论】:

    【解决方案6】:

    从下面的代码中,您可以将 backButton 文本颜色设置为您想要的任何颜色。您可以简单地将 backButton 设置为 clear textColor。因此,它在呈现时将不可见。

     UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.classForCoder() as! UIAppearanceContainer.Type]).setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
    

    更新:如果您想采用不同的方法。请查看此帖子 How to customize the navigation back symbol and navigation back text? 并接受答案。

    【讨论】:

      猜你喜欢
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多