Setting leftBarButtonItem not working - ios

I haver a ViewController in which I'm trying to override the default back button with a custom one. Here is my code:
let myBackButton:UIButton = UIButton.init(type: .custom)
myBackButton.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
myBackButton.setTitle("Back Plz", for: .normal)
myBackButton.setTitleColor(.blue, for: .normal)
myBackButton.sizeToFit()
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
This is not working (I'm still seeing the default back button in the navigation bar). I also tried setting 'backBarButtonItem' and setting 'hidesBackButton = true' and neither worked.
Not sure if it's relevant but this ViewController is being "Push"ed from an embedded View Controller, so my general Storyboard setup is this:
Navigation Controller -> View Controller -> Embedded View Controller -> View Controller (where I'm working)
What am I doing wrong? Thanks!

Related

Unable to press UIButton underneath NavigationBar?

I have an app where there is a navigation controller which can have navigation child. I have to put a UIButton where the navigation bar is normally.
The UIButton is connected to targets:
for button in arrayOfButtons {
button.addTarget(self, action: #selector(buttonTouchUpInsideP3), for: [.touchUpInside])
button.addTarget(self, action: #selector(buttonTouchInsideBoundsP3), for: [.touchDown, .touchDragEnter])
button.addTarget(self, action: #selector(buttonDraggedOutOfBoundsP3), for: [.touchDragExit, .touchCancel]) //there is definitly combining that cud be done here...
}
And basically I can't simply hide the navigationBar as I use it for sizing/autolayout, so what I try to do is: (this code is in the ViewDidLoad of the child navigation controller)
self.navigationController?.navigationBar.isUserInteractionEnabled = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
self.navigationItem.hidesBackButton = true
To remove its UI and have it invisible up there, this part works... however, the issue is that I still cannot press anything in the navigationBar area... even though I do isUserInteractionEnabled = false.
How can I fix this!? So that I can tap a UIButton underneath(?) or rather where the navigationBar is located??
Haven't seen a SO question that has solved this for me :/
Well, for anyone that wants to know:
#StonedStudio was right, I ended up just hiding the navigation bar and instead I simply created a view with the bounds height of the navigationBar anchored to the top of the view.safeAreaLayoutGuide.topAnchor and that way it takes the shape of the would be navigation bar while allowing the tap as well! :)

I can't add a barButton Item to a navigationBar

I Embedded a ViewController in a UINavigationController and after that, I added a new ViewController and connected it with a segue to the first ViewController that is embedded in a UINavigationController. then I wanted to add a BarButtonItem to that second view, but when I let go the bar button item in the top of the view next to the title, it inserts it at the button, and when I run the app it only shows the back button to the first screen and the title. (see the picture of how it looks when running and how it looks after inserting it at the top)
I hope you understood the question and you can help me!
thanks in advance!
Benji
write this code in your viewDidLoad Method of controller where you want to add button
let navBtn = UIButton(type: .custom)
navBtn.setImage(UIImage(named: "side_drawer"), for: .normal)
navBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 100)
navBtn.addTarget(self, action: #selector(openMessagingThread(_:)), for: .touchUpInside)
let rightNavBarItem = UIBarButtonItem(customView: navBtn)
self.navigationItem.setRightBarButton(rightNavBarItem, animated: true)
//Action for barButton
#objc
func openMessagingThread(_ sender: UIButton) {
}

NavigationItem not showing on iPhone 5S/5C size

I have the following snippet to add a "next" button to the top right of my nav bar.
let next = UIButton()
next.setTitle("Next", for: .normal)
next.addTarget(self, action: #selector(nextButtonHandler), for: .touchUpInside)
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: next)
This is showing fine on regular size phone, but NOT on my 5C... I can't figure out why.. I put a breakpoint where the right bar button item is set and it is NOT nil.. Help!
Use this:
let barButton = UIBarButtonItem(title: , style: , target: , action: )
Add the button to the navigationController
super easy

Add new bar button in Navigation Bar with a Tab Bar Controller Swift

I have the next structure in swift 3:
Navigation Controller -> Tab Bar Controller -> 4 View Controllers
I have 3 commons bar buttons items.
My problem is, in just one of the view Controllers I want to add new bar button item on the top-right corner. This way when I navigate to the others views the new button should not appear.
How can I solve this?
Thanks!
You should set it with tabBarController like this in Swift:
let button = UIButton(type: .custom)
button.setImage(UIImage(named: "imageName"), for: .normal)
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
let leftButtonBar = UIBarButtonItem(customView: button)
tabBarController?.navigationItem.rightBarButtonItems = [leftButtonBar].
this is best answer forever and works correctly.
You can have the below structure by which you can have individual
navigation to add button for each view controller
Tab Bar Controller ->Navigation Controller -> 1st View Controllers
->Navigation Controller -> 2nd View Controllers
->Navigation Controller -> 3rd View Controllers
->Navigation Controller -> 4th View Controllers

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

Resources