How to display buttons on navigation bar in UIPageViewController? - ios

I'm using a UIPageViewController connected to a UINavigationController to display two pages, a users table and a chat table.
I'm trying to implement the following:
Two buttons Users and Chat upon tapping which, the corresponding view controller appears. I want these buttons on the navigation bar.
Here's the code I've written in the page view controller class
viewDidLoad()
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let vc1 = storyBoard.instantiateViewControllerWithIdentifier("users")
let vc2 = storyBoard.instantiateViewControllerWithIdentifier("chat")
vcArray = [vc1, vc2]
self.setViewControllers([vc1], direction: .Forward, animated: true, completion: nil)
let button1 = UIButton(frame: CGRectMake(0, 30, self.view.frame.width/2, 30))
let button2 = UIButton(frame: CGRectMake(self.view.frame.width/2+1, 30, self.view.frame.width/2, 30))
button1.setTitle("Users", forState: .Normal)
button2.setTitle("Chat", forState: .Normal)
self.navigationController?.navigationBar.topItem?.titleView?.addSubview(button1)
self.navigationController?.navigationBar.topItem?.titleView?.addSubview(button2)
Unfortunately, none of the buttons appears in the navigation bar.
In fact, I can't even assign a title to the navigation bar. How can I fix this?
Thanks

You can add multiple button on the right side of navigation bar like this
let editImage = UIImage(named: "First")!
let searchImage = UIImage(named: "Second")!
let editButton = UIBarButtonItem(image: editImage, style: .Plain, target: self, action: "didTapEditButton:")
let searchButton = UIBarButtonItem(image: searchImage, style: .Plain, target: self, action: "didTapSearchButton:")
navigationItem.rightBarButtonItems = [editButton, searchButton]
or
self.navigationController?.navigationBar?.navigationItem.rightBarButtonItems = [editButton, searchButton]
If you want to make it look in the centre on the navigation bar, you
can use image with appropriate width so that it scales towards the center.

Related

iOS 11+ Navigation Bar's buttons and title not changing on pushed view controller?

I have 2 view controllers. View Controller A** and View Controller B
I want to achieve a very simple thing. Setting left and right navigation bar buttons on View Controller A and change title and and left navigation bar button on View Controller B when B is pushed.
I design View Controller A as I wanted but I cannot change anything including title on pushed View Controller B.
Here is the code I implemented;
in viewdidLoad of A:
title = "Satışlarını Hızlandır!"
let leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "boostCloseIcon"), style: .plain, target: self, action: #selector(closeButtonTapped))
leftBarButtonItem.tintColor = .black
navigationItem.leftBarButtonItem = leftBarButtonItem
let rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "infoIcon"), style: .plain, target: self, action: #selector(infoButtonTapped))
rightBarButtonItem.tintColor = .black
navigationItem.rightBarButtonItem = rightBarButtonItem
self.navigationController?.navigationBar.barTintColor = .white
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
in viewDidLoad of B:
title = "Confirmation"
let backBarButtonItem = UIBarButtonItem(image: UIImage(named: "infoIcon"), style: .plain, target: self, action: #selector(backButtonTapped))
backBarButtonItem.tintColor = .black
navigationItem.setLeftBarButton(backBarButtonItem, animated: false)

JSQmessageView Controller: Add "Back" Button & Image on Navigation Bar, Swift

I'm using JSQmessageViewController and trying to programmatically add "back" button & user image on the navigation bar. I'm using the following codes. After running it, there's no "back" button or image. Attached is the screenshot of the simulator. I tested these codes with the normal UIViewController, and they worked.
May I know why they don't work with the JSQmessageViewController? And what should I do to add the "back" button & image on the navigation bar? Thanks a lot!
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))
navigationBar.backgroundColor = UIColor.whiteColor()
navigationBar.delegate = self;
let navigationItem = UINavigationItem()
navigationItem.title = strValue
let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = leftButton
let imgButton = UIButton()
imgButton.setImage(image, forState: .Normal)
imgButton.addTarget(self, action: "EditProfile:", forControlEvents: UIControlEvents.TouchDown)
imgButton.frame = CGRectMake(self.view.frame.width - 60, 0, 41, self.view.frame.height)
var rightButton = UIBarButtonItem(customView: imgButton)
self.navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
}
So if you use a navigation controller to present your instantiation of the JSQMessagesViewController then the navigation bar should actually already be there. You can just provide it with those buttons and they will be in the correct place. It also seems that you may be doing things in an outdated syntax. Here is the latest Syntax.
Create a function to add your back button.
func addCancelButtonLeft() {
let button = UIBarButtonItem(barButtonSystemItem: .back, target: self, action: #selector(dismissView))
navigationItem.leftBarButtonItem = button
}
Create the action for the button.
func dismissView() {
dismiss(animated: true, completion: nil)
}
Then for your Image you are actually trying to put a button in that title view. Which is fine.
func titleView() {
let imgButton = UIButton()
imgButton.setImage(image, forState: .Normal)
imgButton.addTarget(self, action: #selector(EditProfile), forControlEvents: .touchUpInside)
navigationItem.titleView = imgButton
}
func EditProfile() {
navigationController?.present(EditProfileViewController(), animated: true, completion: nil)
}
let me know if you have more questions and I will see what I can do. Good luck 👍🏼

How to add button to NavBar while ViewController is embeded in as TabBarController

I try to add refresh button programmatically to my NavBar, while I am in TabBarController and I have a big problem with that. This is my code in ViewDidLoad():
let buttonItem = UIBarButtonItem(barButtonSystemItem: .Refresh, target: self, action: "refreshData")
self.navigationItem.rightBarButtonItem = buttonItem
any suggestions?
This is my storyboard after launch my app (no icon):
Make sure you viewController embed in UINavigationController first then add this navigation controller into tabViewController's view controllers array. After that use the method below:
let refresh = UIButton(frame: CGRectMake(0, 0, 40, 40))
//Set refresh image
refresh.setImage(UIImage(named: "refresh"), forState: UIControlState.Normal)
//or set text title
refresh.setTitle("Refresh", forState: .Normal)
refresh.addTarget(self, action: "refresh", forControlEvents: UIControlEvents.TouchUpInside)
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: refresh)
EDIT:
what's your navBar? It's UINavigationBar, yes, it's possible:
let item = UINavigationItem(title: "")
item.rightBarButtonItem = UIBarButtonItem(title: "Refresh", style: UIBarButtonItemStyle.Done, target: self, action:"refresh")
item.hidesBackButton = true
UINavigationBar().pushNavigationItem(item, animated: false)
But normally it's should be embed in UINavigationController

iOS Navigation Items - toggle programmatically

I am new to iOS (swift) programming. I am creating my first stop watch application. I have added a navigationBar to the MainStoryBoard, and then two navigation items on the bar, one on the left and one on the right. I would like to change the text of left navigation item in the viewDidLoad method and then in an IBAction. Can anyone help me please.
With my .NET coding intellect, I created IBOutlets for each of them and then tried changing their title properties but have failed.
Another similar question regarding the same is how can I toggle the icons on navigation items programmatically.
For UIBarButtonItem with custom button image:
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
For System UIBarButtonItem:
let camera = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("btnOpenCamera"))
self.navigationItem.rightBarButtonItem = camera
You should don't create UINavigationBar on StoryBoard. You should ember it into UINavigationController. And in viewDidLoad you can add NavigationItem programmatically.
Add NavigationBarItem just with text:
self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "left", style: .Done, target: self, action: "leftTap")
With Image:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "backIcon#3x"), style: .Done, target: self, action: "rightTap")
And in Action you can change text or image:
self.navigationItem.leftBarButtonItem?.title = "left change"
self.navigationItem.rightBarButtonItem?.image = UIImage(named: "calendar-icons#3x")
With title and image above just is demo. You can reference to my demo code: Demo Change NavigationItem Programmatically
Hop this help!

How do I keep my navigation Bar and tableView separate as opposed to having my navBar on top of my tableView

I want it so that my navBar doesn't sit on top of my table view but instead is separate from it, so each tableView cell is displayed in equal proportions. As it is, I can't seem to change it, I have tried adding it manually, programtically and tried to change it in the view hierarchy, which I am unable to do. I imagine there is a straightforward solution to this I just can't work out what that is? Below is my code.
override func viewDidLoad() {
super.viewDidLoad()
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
navigationBar.backgroundColor = UIColor.whiteColor()
navigationBar.delegate = self
let navigationItem = UINavigationItem()
navigationItem.title = "Title"
let leftButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Plain, target: self, action: "btn_clicked:")
let rightButton = UIBarButtonItem(title: "Right", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
}
I had the same problem. A solution is just using a standard view controller and then adding table view and navBag. Instead of trying to add a navBar on top of a tableviewcontroller.
Im quite new to swift but i hope this answer helps.

Resources