【问题标题】:Using a UIButton into Navigation Bar using StoryBoard使用 StoryBoard 在导航栏中使用 UIButton
【发布时间】:2020-07-12 06:26:23
【问题描述】:

基本上我在 StoryBoards 中创建了一个 UIButton,我想将它拖到导航栏中。此 UIButton 在触摸时具有与之关联的操作,但是当我将此 UIButton 拖到导航栏中时,所有触摸事件都停止工作。

执行此操作时,我是否需要 IBAction 项的其他代码?

我想向导航栏添加 UIButton 而不是 BarButtonItem 的主要原因之一仅仅是因为它允许我添加背景颜色并弯曲按钮的边缘。

【问题讨论】:

  • 你能展示你的故事板的截图吗?
  • this 有帮助吗?
  • @Sweeper 这看起来正是我需要的我只是不知道如何在 Swift 5 中完成它。你能展示一下它是如何编写的吗?非常感谢您的帮助
  • 我会尽快测试一下

标签: ios swift uibutton


【解决方案1】:

我认为你不能在情节提要中做到这一点。您必须使用 init(customView:) 初始化程序在代码中完成。

let myCustomButtonWithBackgroundsAndStuff = UIButton(type: .custom)
...

// this is the equivalent of connecting the IBAction, in code form, in case you didn't know
myCustomButtonWithBackgroundsAndStuff.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
let barButtonItem = UIBarButtonItem(customView: myCustomButton)
navigationItem.rightBarButtonItem = barButtonItem
// or append to rightBarButtonItems if you have other bar button items

...
@objc func buttonAction() {
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-27
    • 2015-07-02
    • 2016-06-20
    • 2023-04-05
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    相关资源
    最近更新 更多