0

我正在尝试以编程方式自定义导航栏按钮项...我只是在这里遇到了一些问题,我相信这些问题很容易解决。我现在想让它成为默认的添加按钮,但将来也会想让栏按钮项成为我创建的自定义图标。所以我想我想知道如何以编程方式将导航栏按钮更改为默认样式和自定义图标...谢谢!

override func viewDidAppear(animated: Bool) {

    let rightbutton = UIBarButtonItem(title: "+", style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked")

    navigationItem.rightBarButtonItem = rightbutton


}
4

1 回答 1

0

以下是使用默认添加按钮的方法:

    let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "uploadButtonClicked")
    self.navigationItem.rightBarButtonItem = button

以下是以编程方式添加自定义图像的方法:

    let customImage = UIImage(named: "customButtonImage")
    let customButton = UIBarButtonItem(image: customImage, style: UIBarButtonItemStyle.Plain, target: self, action: "uploadButtonClicked:")
    self.navigationItem.rightBarButtonItem = customButton

要执行 segue,您可以执行以下操作:

@IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
    performSegueWithIdentifier("segueIdentifier", sender: self)
}

或者,如果您想从那里转到另一个故事板,那么您的 IBAction 将如下所示:

@IBAction func uploadButtonClicked(sender: UIBarButtonItem) {
    let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
    let nc = storyboard.instantiateInitialViewController() as! UINavigationController
    let vc = nc.viewControllers.first as! YourViewController
于 2015-07-26T02:27:26.240 回答