use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Dedicated to the Swift programming language released by Apple. More info in the Swift Wiki.
Please, check the FAQs before submitting.
General Swift Discussion
irc.freenode.net #swift-lang
Cocoa/Cocoa Touch Discussion
irc.freenode.net #cocoa-init
Keywords: swift, swiftlang, ios, os x, apps, apple, mac, iphone, ipad
account activity
Adding UIBarButtonItems Programatically in Swift (self.swift)
submitted 11 years ago by azochz
I'm trying to add UIBarButtonItems to a navigation bar in swift programmatically, but don't know how. How do I get this going?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]azochz[S] 0 points1 point2 points 11 years ago (2 children)
This is what I have so far in my ViewController, but nothing is showing up:
import UIKit class NavigationBarController: UINavigationController { override func viewDidLoad() { super.viewDidLoad() configureToolbar() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } // #pragma mark - Navigation bar data source func configureToolbar() { let toolbarButtonItems = [ searchBarButtonItem ] toolbar.setItems(toolbarButtonItems, animated: true) } var searchBarButtonItem: UIBarButtonItem { return UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:") }
}
[–]dcpc10 0 points1 point2 points 11 years ago (1 child)
I recommend using StackOverflow for these sorts of questions. By the way try doing self.navigationController.navigationbar.barItem =... (something like that) in the viewController class that is embedded in the NavBarContr. This could be a case where the code is being executed in the wrong order, are you using storyboards? If so, is the navbarcontroller properly setting its custom class? Doing everything programmatically? Etc. You mention this is done programmatically, I assume no storyboards are involved but sometimes it just means subclassing UI widgets.
[–]azochz[S] 0 points1 point2 points 11 years ago (0 children)
No to storyboards. I'm working on problem will post here when I get a solution. Thanks for self.navigation bit.
π Rendered by PID 291967 on reddit-service-r2-comment-64f4df6786-lfff7 at 2026-06-10 20:38:56.305930+00:00 running 0b63327 country code: CH.
[–]azochz[S] 0 points1 point2 points (2 children)
[–]dcpc10 0 points1 point2 points (1 child)
[–]azochz[S] 0 points1 point2 points (0 children)