[英]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.