Editing Bar Buttons with PushSegue iOS - ios

I want to have barButtonItems in the navigation bar of a view which I arrive at through a pushSegue.
I tried to achieve this by adding a navigation bar item to my storyboard, but when I launch the app, the barButton doesn't show in the build. See the screenshots here:
How the navigation bar looks on build
How I built it in Storyboards
Flow of the Storyboard
I segue to my MainTextView by clicking on a cell using this code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)
if let destination = storyboard?.instantiateViewController(withIdentifier:"MainTextView") as? MainTextView {
destination.post = (posts[indexPath.row])
destination.delegate = self
navigationController?.pushViewController(destination, animated: true)
}
}
}
The back button doesn't even show up in the Storyboard. How could I change the default back button and add additional buttons to the navigation bar programmatically?

I assume this might be a Xcode 11/ios 13 related issue. Click on the segue and change it from push to present modally. From there change presentation context to fullscreen. You might be able to leave the it as push but when you click on your your viewcontroller/navigation controller go to the attributes inspector and look for a option called presentation, and set it to full screen.
Another option to try is to click on the view controllers, go to attributes inspector and under navigation controller - bar visibility tap (shows navigation bar)
Last thing I can think of is that when your segue to and from should be connected to the navigation controller.
Actually a few more possibilities...
1) check your debug view hierarchy when you launch the app. What I mean by this is
https://developer.apple.com/library/archive/documentation/ToolsLanguages/Conceptual/Xcode_Overview/ExaminingtheViewHierarchy.html
basically its a button you click and you'll be able to see how the views are stacked onto of each other. My best guess is that while you have a bar item button there, the nested button is not actually present/showing.
2) nest your tableviewcontroller in another navigation controller.

Related

Two NavigationBar Showing

Hi I am new to Swift and am trying to build an app with multiple Views..
My first View(initial view) is embedded in navigation controller.
My Second View is embedded in Tab Bar Controller
My Third View is again embedded in a Navigation Controller.
The problem is that on my third view I see to navigation Controller with the top one taking me back to First View and the below one taking me to Second View.
Is it an incorrect way of doing this? I want to get rid of navigation bar that came from 1st view.
Thanks in anticipation.
PS : I had initially not attempted Navigation Bar on 3rd View.. but the problem was that I am also not able to map Bar Button Item and hence to embed the 3rd View too in a separate Navigation Controller
While it shows perfect in Xcode.. it shows 2 NavBar on the simulator
Not an elegant solution but still this can solve your problem. On your controller embed to UITabBarController where you have added Next Button. Add the below code on that controller class.
On ViewWillappear add show nav bar and on viewDidDisappear hide nav bar as shown in below code
ON viewWillAppear:
override func viewWillAppear(_ animated: Bool) {
self.navigationController.navigationBar.isHidden = false
}
ON viewDidDisappear:
override func viewDidDisappear(_ animated: Bool) {
self.navigationController.navigationBar.isHidden = true
}

Tab Bar disappears on segue

What I want to achieve:
I have a "Login" ViewController which segues to a Tab Bar Controller.
My Tab Bar Controller goes to a Navigation Controller and then "Home" ViewController with a Table View. This view is my "Home/Welcome" page. I want there to be a Tab Bar at the bottom as well as a static table which I can edit with Prototype Cells. Each cell goes to a different ViewController.
Then, I want to be able to tap each Cell (I have "Info" setup for now) and it segue to the corresponding ViewController. For now, the "Info" cell should go to the second Navigation Controller and then "Information" ViewController.
From there, I want another static table which I can edit with Prototype Cells. Each cell will go to a seperate stack of ViewControllers.
My problem:
When I segue from the "Info" cell in the "Home" ViewController, the Information tab bar "pushes" up, and the Tab Bar disappears. I've experimented with other segue forms but none of them maintain the Tab Bar.
I want it so you can use either the Tab Bar to navigate to the Info ViewController or the cell on the Home/Welcome ViewController to navigate to the Info page.
I have no code at all at this point, I am just using these segues.
Does anyone have any idea how I can achieve what I want to achieve, and if so, provide as much sample code as I'm extremely new to Swift.
Any help is appreciated! Thanks :)
Don't add segue to open the "Information" ViewController which is part of UITabBarController.
You must have used didSelectRowAt for cell selection in tableview. Just add one line in it as it is :
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true) {
self.tabBarController?.selectedIndex = 1
}
This will change the tab and open the "Information" ViewController.
If you want to load the "Information" ViewController with data relevant to cell selection you can go with Protocols or NotificationCentre which can pass data to "Information" ViewController from Homeviewcontroller on cell selection.

Navigation controller with tab bar controller?

I have a tableViewController embedded in my tab bar controller. When a cell is tapped, a segue is launched to another view controller to show the details of that object. However, the Back button is not appearing in the viewDetail. I tried embedding the view into a separate Navigation Controller but that didn't change anything. What am I doing wrong? I currently have Tab Bar Controller -> tableView -> Navigation Controller -> viewDetail (need Back button here to return to tableView).
Here's what I have right now:
Thanks!!
Each UIViewController in UITabBarController could be embedded in an UINavigationController at your convenience, that way you'll be able to use all of the features that you need.
Basically, you need to select the tableViewController, click on Editor menu item, select Embed in and click on Navigation Controller, ta daa.
You can show or hide Navigation Bar if you need it using Interface Builder or programmatically in your Detail viewController as follows:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBarHidden = true
// Do stuff
}
Set NavigationController to TabBarController, then set NavigationController's rootViewController to TableViewController.
You just have the organization wrong. Currently you have Tab Bar Controller -> tableView -> Navigation Controller -> viewDetail. It should be Tab Bar -> tableview -> View detail. Navigation should be separate pointing to table view. Nothing should be pointing to navigation. It should just point to tableview
It should look something like the above picture

Adding a navigation controller programmatically for the second VC

In my storyboard, I have setup a navigation controller points to my MainVC, and it works just fine.
And now, I'm trying to add another view called "HelpVC", and I created one in the storyboard. It automatically shows the navigation bar on the top.
(MainVC segues to HelpVC)
However, I did everything else in code.
I had initWithView in the HelpVC which draws out the interface, BUT the navigation bar does not show up, so I can't go back to that previous view controller.
How do I make sure that the navigation bar shows up and works just like other view controllers? (segue back to the last view?)
It is not very clear from the post, but as I understood, you may want to try:
override func viewWillAppear(_ animated: Bool){
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}

Pop up a View from a Button containing information but keeping the current View behind it: Swift implementation

I am finishing my career project and I want to make some UI improvements on the application I developed in Swift for iOS.
The application does not have any explicit explanation of how it works and I'd like to detail it in a pop-up View called from an information button in the Navigation Bar. I'd like some kind of View similar to the Notification Alerts, which pop-up in front of the current view and they disappear by pressing another button. I give you an example image:
I want this button to be available from any Tab View (I've got three views). Any idea of how to manage that?
Thank you very much!
First, go to IB and create a new View Controller. In the Attributes Inspector, click on the dropdown next to Size and click Freeform. Set the size to what you want. Then click Use Preferred Explicit size.
Control-drag from your info button to the new VC you created. Click on the segue, then go to the attributes inspector and change the segue type to 'Present As Popover.' Change the segue identifier to whatever you want. I named it popover.
Create a new class for your popover view and assign it to the VC you created. It doesn't need any code at the moment.
In your presenting VC (Tab Bar), add UIPopoverPresentationControllerDelegate to your class definition. Then add this function:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
Next, in your Tab Bar VC, add this code to your prepareForSegue method:
if segue.identifier == "popover" {
let controller = segue.destinationViewController as! PopoverVC // whatever you named the class
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
controller.popoverPresentationController!.delegate = self
}
That should do it.

Resources