【问题标题】:How to create a button and segue from titleView image如何从titleView图像创建一个按钮和segue
【发布时间】:2017-09-11 10:11:15
【问题描述】:

在自定义导航栏时遇到问题。我已通过编码将 titleView 文本更改为图像。现在我希望图像成为一个可点击的按钮,并与起始页面相连(例如主视图控制器的主页按钮)。如何将 titleView 图像制作成按钮(或项目)以及如何从中创建 segue。我想它必须在代码中完成,因为我还没有找到任何其他方法来做到这一点。如果您有解决方案,请彻底 - 我是初学者。

【问题讨论】:

  • 欢迎来到 SO,能否请您添加您的代码,以便我们看到您所做的工作?

标签: ios swift uibutton uinavigationbar segue


【解决方案1】:

只需将包含UIButtonUIImageView 的自定义UIView 添加到navigationItemtitleView

示例:

override func viewDidLoad()
{
    super.viewDidLoad()

    let button = UIButton(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 80, height: 30))
    button.setTitle("Button", for: .normal)
    button.addTarget(self, action: #selector(showMainVC), for: .touchUpInside)

    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 80, height: 30))
    imageView.image = UIImage(named: "Image")

    let customView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width - 80, height: 30))
    customView.addSubview(imageView)
    customView.addSubview(button)
    self.navigationItem.titleView = customView
}

func showMainVC()
{
    //TODO:
}

【讨论】:

  • 太棒了!坦克为您提供帮助! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
相关资源
最近更新 更多