Navigation Left Bar Button Item as Image Logo - ios

I'm trying to add an image to a left on a Navigation Bar using leftBarButtonItem that will serve as a small logo. I'm able to add the image perfectly fine. My question is how do I disable it from being clickable and just keep the same state so it doesn't turn gray when someone presses it. I just want it as an image in the left of the Navigation Bar. Below is what I use to add the logo to the left of the navigation bar. Any help is greatly appreciated.
let btnName = UIButton()
btnName.setImage(UIImage(named: "NavIcon"), forState: .Normal)
btnName.frame = CGRectMake(0, 0, 40, 50)
let leftBarButton = UIBarButtonItem()
leftBarButton.customView = btnName
self.navigationItem.leftBarButtonItem = leftBarButton

btnName.userInteractionEnabled = false

Related

image and text inside UITabBarItem

I'm trying to add a text and an image inside a UITabbarItem but it didn't work when I used UITabBarItem inside the Navigation Bar.
So, I have added UIButton inside the Navigation Bar and set image and title for it like this:
Click to see example image
Here is what I did :
let button = UIButton(type: .system)
button.setImage(UIImage(named: "sortIc"), for: .normal)
button.setTitle("ترتيب",for: .normal)
button.sizeToFit()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: button)
What I want to do is to change the place of the image to the text place and vice versa. Any suggestions ?

How do I Add a UIBarButtonItem to the navigationBar in a turbolinks-ios app

I have a turbolinks-ios app that I've inherited and need to add a native nav.
At first I used the storyboard to embed the main application controller within a navigation controller, but turbolinks has it's own navigation bar so that creates two stacked navigation bars. I then deleted the nav controller from the storyboard, but now I cannot add my button to the default turbolinks nav bar. There's no errors, and if I step through the code it hits every line within my function, but nothing is displayed in the bar. What am I doing wrong?
My function which is called from viewDidLoad() within the UINavigationController class is as follows...
func addSlideMenuButton() {
let callback = #selector(toggleNav)
let btnShowMenu = UIButton(type: UIButtonType.system)
btnShowMenu.setImage(self.defaultMenuImage(), for: UIControlState())
btnShowMenu.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
btnShowMenu.addTarget(self, action: callback, for: UIControlEvents.touchUpInside)
let customBarItem = UIBarButtonItem(customView: btnShowMenu)
navigationController?.navigationItem.rightBarButtonItem = customBarItem
}
Any nudge in the right direction would be greatly appreciated.
You have to call sizeToFit on the customView first.
So add btnShowMenu.sizeToFit just before creating the UIBarButtonItem.

SWRevealViewController with NavigationController - Swift 3

I want to make an app in which my navigation bar appears on all pages. When I click on any row of side menu(done through SWRevealViewController), I want the page that opens to have a navigation bar on top. In the image below, I want the same navigation bar as the HomeViewController on the page that has "Menu" label. How can I do this? Please help. I am new to iOS. I am doing this in Xcode 8 and Swift 3.
EDIT : I want something like this: I have placed the side menu button on the reveal view controller. I can see it on the front view controller at runtime but how to connect the target and action of revealviewcontroller then, so that the side menu opens? If this is done then my problem of navigation controller on "Menu" label page will be solved automatically
Suppose outlet of menu is btn_Menu. In controller's view did load I set bar button's action and target programmatically.
btn_Menu.target = self.revealViewController()
btn_Menu.action = #selector(SWRevealViewController.revealToggle(_:))
Have a look at this image structure that you need. Sorry not very clear but perhaps solve your issue:
You need to add all the starting points of your ViewControllers in a NavigationController of their own so in this case you need to embed the ViewController which has the menu label in a NavigationController and make the side menu segue to the NavigationController not the ViewController
hope my answer helps you
1) create a new ViewController Class called 'BaseViewController'
2) in the BaseViewController viewDidLoad add the following code and change the images names and targets also add the left button for the menu
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)
3) make the viewControllers which you will show from the side menu like the one with menu label inherits from BaseViewController and in it's viewDidLoad make it call super.viewDidLoad()
4) embed the viewControllers which you will show from the side menu in a navigationController
Using Container View may help.
Replace the UIView in the middle (Content) with a Container View and that container can be a UINavigationController. And to avoid having 2 NavigationBars you can hide the NavigationBar of the ContainerView.
The blue highlighted view is the ContainerView inside the Controller that is implementing SideMenu
You also need to add Navigation Controller for sabe bar Controller
Follow the steps :
Select side bar viewcontroller > go to Editior menu > Embed in > Click Navigation Controller

Add image to Navigation Bar Back Button

I want to have the back button with "< [IMG]" instead of "< Back" which is by default.
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
with this I have removed the text but how to add an image next to the "<".
let backButton = UIBarButtonItem(image:UIImage(named: "ic_authors_detail_back.png"), style:.Plain, target:self, action:"backButtonPressed");
backButton.tintColor = UIColor(hexString: "#ffffffff")
let leftBarButtonsArray = [backButton]
navigationItem.leftBarButtonItems = leftBarButtonsArray
Here is the code to add a button on navigation bar.Hope this helps
When you click left bar button
You can set its attribute like:
Here is option to set image.

hide and show a ui nav bar button

I am having a situation in which I have to hide the button on the right side of nav bar. Button name is btnRefresh, and I can hide it successfully by this way
self.nvbar.topItem?.rightBarButtonItem = nil
but when I use this for showing it again, it didn't get displayed again
self.nvbar.topItem?.rightBarButtonItem = btnRefresh
Any help???
You need to change the tint color to clear and disable the button as the following :
let barButtonItem = UIBarButtonItem()
barButtonItem.tintColor = .clear
barButtonItem.isEnabled = false
And to display it again change the color and enable it again :
barButtonItem.tintColor = .black
barButtonItem.isEnabled = true

Resources