UIBarButtonItem in navigation bar programmatically?
Custom button image without setting button frame:
You can use init(image: UIImage?, style: UIBarButtonItemStyle, target: Any?, action: Selector?)
to initializes a new item using the specified image and other properties.
let button1 = UIBarButtonItem(image: UIImage(named: "imagename"), style: .plain, target: self, action: Selector("action")) // action:#selector(Class.MethodName) for swift 3
self.navigationItem.rightBarButtonItem = button1
Check this Apple Doc. reference
UIBarButtonItem with custom button image using button frame
FOR Swift 3.0
let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "imagename"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn1.addTarget(self, action: #selector(Class.Methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "imagename"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btn2.addTarget(self, action: #selector(Class.MethodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setRightBarButtonItems([item1,item2], animated: true)
FOR Swift 2.0
and older
let btnName = UIButton()
btnName.setImage(UIImage(named: "imagename"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 30, 30)
btnName.addTarget(self, action: Selector("action"), forControlEvents: .TouchUpInside)
//.... Set Right/Left Bar Button item
let rightBarButton = UIBarButtonItem()
rightBarButton.customView = btnName
self.navigationItem.rightBarButtonItem = rightBarButton
Or simply use init(customView:) like
let rightBarButton = UIBarButtonItem(customView: btnName)
self.navigationItem.rightBarButtonItem = rightBarButton
For System UIBarButtonItem
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera
For set more then 1 items use rightBarButtonItems
or for left side leftBarButtonItems
let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
let item1 = UIBarButtonItem()
item1.customView = btn1
let btn2 = UIButton()
btn2.setImage(UIImage(named: "img2"), forState: .Normal)
btn2.frame = CGRectMake(0, 0, 30, 30)
btn2.addTarget(self, action: Selector("action2:"), forControlEvents: .TouchUpInside)
let item2 = UIBarButtonItem()
item2.customView = btn2
self.navigationItem.rightBarButtonItems = [item1,item2]
Using setLeftBarButtonItem
or setRightBarButtonItem
let btn1 = UIButton()
btn1.setImage(UIImage(named: "img1"), forState: .Normal)
btn1.frame = CGRectMake(0, 0, 30, 30)
btn1.addTarget(self, action: Selector("action1:"), forControlEvents: .TouchUpInside)
self.navigationItem.setLeftBarButtonItem(UIBarButtonItem(customView: btn1), animated: true);
For swift >= 2.2 action should be
#selector(Class.MethodName)
... for e.g.btnName.addTarget(self, action: #selector(Class.MethodName), forControlEvents: .TouchUpInside)
Adding a UIBarButtonItem programmatically
you are adding UIBarButtonItem to a new instance of NavigationController. so it will not appear there.
so in your view controller which you want to handle right navigation bar , under one of these methods:
override func viewDidLoad()
or
override func viewWillAppear
add:
let rightBarButton = UIBarButtonItem(title: "LogIn", style: .plain, target: self, action: #selector(logInPressed))
self.navigationController?.navigationItem.setRightBarButton(rightBarButton, animated: true)
How to programmatically set action for barButtonItem in swift 3?
ex:-
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
How can I add UIBarButtonItem to my navigation controller programmatically?
With image:
self.navigationItem.setLeftBarButtonItem( (UIBarButtonItem(image:UIImage(named:"image"), style: .Plain, target:self, action:"action:")), animated: false)
With title:
self.navigationItem.setLeftBarButtonItem( (UIBarButtonItem(title:"backButton", style: .Plain, target:self, action:"back_pressed:")), animated: false)
Furthermore, you can assign multiple items for each side:
var test: UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"test"), style: UIBarButtonItemStyle.Plain, target: self, action: "test1:")
var test2: UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"test2"), style: UIBarButtonItemStyle.Plain, target: self, action: "test2:")
navigationItem.rightBarButtonItems = [test1, test2]
When using multiple items you might be interested in:
var spacing : UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
which is used to have similar space between buttons.
Add button to navigationbar programmatically
Inside my UIViewController
derived class, I am using the following inside viewDidLoad
:
UIBarButtonItem *flipButton = [[UIBarButtonItem alloc]
initWithTitle:@"Flip"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(flipView:)];
self.navigationItem.rightBarButtonItem = flipButton;
[flipButton release];
This adds a button to the right hand side with the title Flip, which calls the method:
-(IBAction)flipView
This looks very much like you #3, but it is working within my code.
UIBarButtonItem isn't shown in combination with large title inside navigation bar
You should not set navigation item of NavigationController rather you should set the navigation item of your ViewController which has embedded UINavigationController
to it.
So in your ViewController's viewDidLoad
write
override func viewDidLoad() {
super.viewDidLoad()
let upButton = UIBarButtonItem(image: UIImage(systemName: "arrow.up"), style: .plain, target: self, action: #selector(handleUpButtonTapped))
self.navigationItem.rightBarButtonItem = upButton
}
Or in your case, simply write
private func configureBarButtons() {
let upButton = UIBarButtonItem(image: UIImage(systemName: "arrow.up"), style: .plain, target: self, action: #selector(handleUpButtonTapped))
self.navigationItem.rightBarButtonItem = upButton
}
Adding System Image to navigation bar programmatically
You are using the wrong UIImage
initializer. The one you are trying to use requires a full path to an image file. You want UIImage(systemName:)
.
let button = UIBarButtonItem(image: UIImage(systemName: "magnifyingglass.circle") , style: .plain, target: nil, action: nil)
Set UIBarButtonItem target programmatically to previous view Controller
I already found the solution. Instead of using self.navigationController to show the previous controller, I used the general present command to show the view controller.
This is my code, and it solved all of my problems.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ParentViewController")
self.present(vc!, animated:true, completion:nil)
I hope it will help the others who facing a problems like me.
Create NavBar programmatically with Button and Title Swift
Updated for Swift 5
Create a navigation item instance and set title and right/left buttons to it. After navigation item is configured add it to the navigation bar.
let navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: 44))
view.addSubview(navBar)
let navItem = UINavigationItem(title: "SomeTitle")
let doneItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(selectorName:))
navItem.rightBarButtonItem = doneItem
navBar.setItems([navItem], animated: false)
Related Topics
Determine If the Access to Photo Library Is Set or Not - PHPhotolibrary
How to Add a Toolbar Above the Keyboard
Uialertview First Deprecated iOS 9
How to Delete All Cookies of Uiwebview
How to Set Up Push Notifications in Swift
Resize Uiimage to 200X200Pt/Px
How to Draw a Transparent Uitoolbar or Uinavigationbar in iOS7
Add Bottom Border Line to UI Textfield View in Swiftui/Swift/Objective-C/Xamarin
Converting iPhone Xib to iPad Xib
Instruments Allocations Track Alloc and Dealloc of Objects of User Defined Classes
Uibarbuttonitem in Navigation Bar Programmatically
Objective C - Assign, Copy, Retain
Loading a Reusable Uitableviewcell from a Nib
How to Call Gesture Tap on Uiview Programmatically in Swift