ios Swift 3.0 向导航栏添加右键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45069477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:19:29  来源:igfitidea点击:

Swift 3.0 Adding a Right Button to Navigation Bar

iosswiftbuttonnavigationbar

提问by Kevin

I have added a navigation bar to the top of a view controller. I am trying to control whether a button is visible based a condition, but I am having trouble adding the button. So far I have,

我在视图控制器的顶部添加了一个导航栏。我试图根据条件控制按钮是否可见,但我在添加按钮时遇到了问题。到目前为止,我有,

var addButton: UIBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))

override func viewDidLoad() {
    super.viewDidLoad()

    let boool = true
    if boool {
        self.navigationItem.rightBarButtonItem = self.addButton
    }
    else {
        self.navigationItem.rightBarButtonItem = nil
    }
}

func addTapped(sender: AnyObject) {
    print("hjxdbsdhjbv")
}

I believe it is not working properly because I have added a navigation bar into the VC, instead of using a navigation controller and working with the bar there. I was wondering if there was a way to work with this navigation bar.

我相信它不能正常工作,因为我在 VC 中添加了一个导航栏,而不是使用导航控制器并在那里使用导航栏。我想知道是否有办法使用此导航栏。

采纳答案by Kyle H

You say you added a UINavigationBarto your view controller via storyboard, but looking at the code you provided there is no outlet connection to your navigation bar in IB.

您说您UINavigationBar通过情节提要向视图控制器添加了 a ,但是查看您提供的代码,IB 中的导航栏没有出口连接。

In order to access self.navigationItemyour view controller must be embedded in a UINavigationControlleror be part of a hierarchy which is. Unless you have a need for a custom navigation bar on an individual view controller, I suggest removing that from Interface Builder, then making sure either the view controller in question is embedded in a UINavigationControlleror it is being pushed onto the navigation stack from another controller which is embedded in a navigation controller and then you should see your UIBarButtonItem.

为了访问self.navigationItem您的视图控制器必须嵌入到UINavigationController层次结构中或成为层次结构的一部分。除非您需要在单个视图控制器上自定义导航栏,否则我建议将其从 Interface Builder 中删除,然后确保有问题的视图控制器嵌入在 a 中,UINavigationController或者从另一个控制器推送到导航堆栈上嵌入在导航控制器中,然后您应该会看到您的UIBarButtonItem.

回答by Mannopson

It's simple. Put this line of code to the viewDidLoad:

这很简单。把这行代码放到viewDidLoad

self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "test", style: .done, target: self, action: #selector(addTapped))

Updated for Swift 4or later:

为 Swift 4或更高版本更新:

A custom function:

自定义函数:

@objc func action(sender: UIBarButtonItem) {
    // Function body goes here
}

(Custom) Right bar button item:

(自定义)右侧栏按钮项:

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))

(Custom) Left bar button item:

(自定义)左栏按钮项:

self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "some_text", style: .done, target: self, action: #selector(self.action(sender:)))

Also you can add a system bar button items something like this: UIBarButtonItem.SystemItemDefines system-supplied images for bar button items: .add, .done, .cancel, .edit, .save, .compose, .reply, .organizeand more.

您还可以添加系统栏按钮项目,如下所示:UIBarButtonItem.SystemItem为栏按钮项目定义系统提供的图像:.add、.done、.cancel、.edit、.save、.compose、.reply、.organize等。

(System) Right bar button item:

(系统)右侧栏按钮项:

self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))

(System) Left bar button item:

(系统)左栏按钮项:

self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(self.action(sender:)))

回答by Urvish Modi

let rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "EditImage"), style: .done, target: self, action: #selector(ViewController.call_Method))

self.navigationItem.rightBarButtonItem = rightBarButtonItem

回答by ikbal

Swift 4.2;

斯威夫特 4.2;

Add viewController

添加 viewController

override func viewDidLoad() {
        super.viewDidLoad()
        self.addNavigationBarButton(imageName: "ic_back", direction:.left)
 }

Add Classyour API or Utility Class

添加Class您的 API 或实用程序类

public func addNavigationBarButton(imageName:String,direction:direction){
    var image = UIImage(named: imageName)
    image = image?.withRenderingMode(.alwaysOriginal)
    switch direction {
    case .left:
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
    case .right:
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: image, style:.plain, target: nil, action: #selector(goBack))
    }
}

@objc public func goBack() {
    self.navigationController?.popViewController(animated: true)
}

public enum direction {
    case right
    case left
}

回答by Stefan S

Firstly, you need to connect you navigation bar to an IBOutlet so that you can refer to it in your code. After that, this code should work:

首先,您需要将导航栏连接到 IBOutlet,以便您可以在代码中引用它。之后,此代码应该可以工作:

    let navigationItem = UINavigationItem(title: "Title")
    navigationItem.rightBarButtonItem = self.addButton
    navigationItem.hidesBackButton = true
    self.navigationBar.pushItem(navigationItem, animated: false)

回答by akr ios

tested in Xcode 10.2, swift 5.0; First, I have have embedded my ViewController in UINavigationController in IB. Then in ViewDidLoad include these lines

在 Xcode 10.2、swift 5.0 中测试;首先,我已经在 IB 的 UINavigationController 中嵌入了我的 ViewController。然后在 ViewDidLoad 中包含这些行

self.title = "orange"
 self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(changeLayout)).

Note - accessing title, or adding button through navigation controller did not work. For example : setting title - Self.navigationcontroller.navigationItem.title did not work ,

注意 - 通过导航控制器访问标题或添加按钮不起作用。例如:设置标题-Self.navigationcontroller.navigationItem.title 不起作用,