简体   繁体   English

使用 StoryBoard 将 UIButton 用于导航栏

[英]Using a UIButton into Navigation Bar using StoryBoard

Essentially I have a UIButton created in StoryBoards that I would like to drag into the navigation bar.本质上,我在 StoryBoards 中创建了一个 UIButton,我想将其拖到导航栏中。 This UIButton has an action associated with it when touched but all touch events stop working when I drag this UIButton into the navigation bar.这个 UIButton 在触摸时有一个与之关联的动作,但是当我将此 UIButton 拖到导航栏中时,所有触摸事件都会停止工作。

Is there additional code I need to the IBAction item when doing this?执行此操作时,IBAction 项是否需要其他代码?

One of the main reasons I want to add a UIButton instead of a BarButtonItem to the navigation bar is simply because it allows me to add a background color and curve the edges of the button.我想向导航栏添加 UIButton 而不是 BarButtonItem 的主要原因之一仅仅是因为它允许我添加背景颜色并弯曲按钮的边缘。

I don't think you can do it in the storyboard.我不认为你可以在故事板中做到这一点。 You have to do it in code, using the init(customView:) initialiser.您必须使用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() {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 情节提要中导航栏标题视图中的UIButton和标签 - UIButton and Labels in Navigation Bar Title View in Storyboard 通过代码生成导航栏视图(不使用情节提要) - Generate Navigation Bar View by Code (Not using Storyboard) 使用情节提要添加到导航栏的开关 - Add a Switch to Navigation Bar using Storyboard 如何使用情节提要在UIPopover中具有导航栏? - How to have Navigation Bar in UIPopover using Storyboard? 如何使用情节提要在消失的导航栏上添加栏按钮项? - How to Add a Bar Button Item on a Disappeared Navigation Bar using Storyboard? 使用Storyboard更改导航栏中的后退按钮标签? - Change back button label in navigation bar using Storyboard? Swift iOS不确定使用Storyboard ID时如何向Storyboard中的VC添加导航栏 - Swift ios not sure how to add navigation bar to VC in storyboard when using storyboard id 以编程方式创建UIButton比使用storyboard模糊 - creating UIButton programmatically is blurry than using storyboard 如何在不使用导航控制器或导航栏的情况下从Xib返回main.storyboard中的视图控制器? - How to return to view controller in main.storyboard from a xib without using navigation controller or navigation bar? UIButton不在导航栏中 - UIButton is out of navigation bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM