I have a (subclass of) UITabbarController with 4 tabs, every tab has a (subclass of) UINavigationController with a 3+ levels view controllers navigation hierarchy.
The tab bar is visible only in each UINavigationController root view controller.
When the UINavigationController pushes the second level view controller the tab bar slides out correctly with the root view controller:
Until here no problem, everything is correct.
Step 2, completely custom Tab Bar
I have implemented a floating tab bar (subclass of UIView not UITabBar), added the tab bar to the UITabbarController view (note: the default tab bar is added to the same view) but the behaviour when the navigation controller pushes the second navigation level is different, the tab bar remain visible :
Obviously the UINavigationController manage the standard tab bar and move it with the root view controller, any idea how to replicate this behaviour with my custom tab bar? I'm looking for a smart and reusable way, not do it manually in each root view controller.
Additionally I have created a MyTabBar : UITabBar class and changed the class type of the UITabbarController navigationBar in the Storyboard in order to intercept the setHidden method and hide my custom tab bar:
- (void)setHidden:(BOOL)hidden {
if (hidden)
[[NSNotificationCenter defaultCenter] postNotificationName:kNOTIF_TabBarNotificationHideBar object:nil];
else
[[NSNotificationCenter defaultCenter] postNotificationName:kNOTIF_TabBarNotificationShowBar object:nil];
[super setHidden:YES];
}
that works correctly.
Related
I know that on the view you can add the following modifiers to modify the navigation bar to create a custom back button.
SomeView{ ... }
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
mode.wrappedValue.dismiss()
} label: {
Image(systemName: "chevron.backward")
}
}
However, I do not want to go through my entire app to every secondary view and add these modifiers. I've considered creating a reusable header component, but at the moment, I'm just wondering if there was a way to override the system default for the back button to impact the entire app.
Unfortunately In SwiftUI there is not. You could however, override UINavigationController but it is not recommended as APIs can change.
Look at List for example, we used to set the UITableView appearance background color to .clear to customize List's background, but in iOS 16 this solution works no more.
Extended View and create a func where you put your code in it, then use that function wherever you need!
I have added SwiftUI views in my existing swift project.
I have the following flow in the project
FirstViewController --NavigationPush -> SwiftUIView1 -NavigationLink->
SwiftUIView2 - NavigationLink-> SwiftUIView3 - NavigationLink
(UIViewRepresentable) -> SecondViewController.
till SwiftUIView3 everything works as expected. while pushing from SwiftUIView3 -> SecondViewController, even though I'm setting the navigation property isHidden = false,
the navigationBar appears in the controller for a fraction of sec and disappears. I have tried to unhide navigation in all the controller life cycle methods. But nothing happens. Please let me know your suggestion to resolve this.
FYI, below code, I used SwiftUI to hide the navigation bar.
Code Block
.navigationBarTitle("") // This should be empty.
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
In UIKit-based application we can have custom navigation transitions using UIViewControllerAnimatedTransitioning protocol.
Is there an equivalent in SwiftUI?
I know we can already animate between removing and adding views.
But how do we do this when we push and pop to the navigation stack?
There isn't anything like this available in SwiftUI's APIs so far afaik. Make sure to open an enhancement request with Apple.
Implementing "navigation" on your own is a terrible idea, as you basically give all the accessibility and facility support afforded by UINavigationController.
Instead, here is a suggestion:
Either by means of a custom SwiftUI view or modifier, wrap the NavigationView with a UIViewControllerRepresentable wrapper, which in turn sets up a UIHostingController subclass, which waits for addChild(_:) to be called, which will be the navigation controller added as a child of the hosting controller. From here, if your animations are "static" (e.g. do not require any subview to subview transition), you can accomplish this here by implementing the navigation controller delegate, and providing the animation controller.
If you do need more evolved transitions (such as Photos.app's photo transition), you can create a custom UIViewRepresentable wrapper, which will serve as markers for "from" views and "to" views, you can then use those discover in UIKit, and e.g. can snapshot and animate in transitions.
Proposed API can look like this:
struct ContentView1: View {
var body: some View {
NavigationLink(destination: DetailView()) {
Image("small").customSourceTarget()
}.navigationBarTitle("Navigation")
}
}
struct ContentView2: View {
var body: some View {
Image("large").customTransitionTarget()
}
}
NavigationView {
ContentView1()
}.customAnimatable()
newBie here. I have added an UItableViewController into storyBoard. I use this as a setting page.
HomeVC ----> Setting VC
In code : I use the below code to bring the tableView below the battery Bar:
self.tableView.contentInset = UIEdgeInsets(top: 20,left: 0,bottom: 0,right: 0)
Problem:
How to move the TableView down below the battery bar so that I can add a button above the table in StoryBoard
Please help.
For this you should use a normal view controller and drag a table on the view controller this way you can make the table size whatever you like. This will also allow you to place buttons wherever you like.
Don't forget to assign the TableViewDataSource and TableViewDelegate to your view controller.
Good luck!
After doing some search on UiTableViewController, to use it as setting page I have to set static cell to it. This UiTableViewController will occupy the entire view.
To set the tableView below the battery icon,
use:self.tableView.contentInset = UIEdgeInsets(top: 20,left: 0,bottom: 0,right: 0)
However, No need to do so. In the first TableViewSection, you can create a few tableView cell, just leave the first cell blank which acts as a margin below the battery bar.
Swift 3/iOS 10/Xcode 8
I have a view controller (pieChart) that contains a label, two buttons and an empty view (which will contain a pie chart). The label and two buttons are incorporated into a horizontal stack view, which lies above the pie chart view.
The above VC is embedded into one of four container views (The main screen of the app is comprised of these four container views) when the app starts up.
In pieChart, I have linked both buttons up to their respective IBActions and IBOutlets. When clicking on button 2, a modal segue should occur to another VC but this does not happen. Visibly, the button is registering the click - ie it changes colour when you click it. I have placed a print statement in the IBAction method for button 2 but this too does not display. No error messages are displayed in console either.
The only reasons for this occurring that I have found after several hours of hunting are:
sub views have been added to the button itself so the click event signal passes by the button to be received by the added sub views. This is not the case for me. Order is Main App Window > Container View > Embedded VC > Stack View > Button. InteractionEnabled is set to true for all.
Button lies partially outside containing view (it's height/width might be greater than its containing view). This is not the case for me. The stack view, label and both buttons share the same height and the width of the stack view is equal to the sum of the widths of the label and buttons plus the spacing between the label and buttons.
I have also tried adding an event handler programmatically with:
SelectAnalyisButtonOutlet.addTarget(self, action: #selector(SelectAnalysisButtonClicked), for: .touchUpInside)
but the same outcome occurs.
Are there any other reasons for the click events seemingly not registering?
EDIT 1
The pieChart VC mentioned above is one of several VC's that are swapped out of the same container view (called detailContainerView), depending on which button (all of which work just fine) is clicked in one of the OTHER container views (called TabBar).
I placed a button in each of two other VC's that get displayed in detailContainerView and hooked them each up to an IBAction. Each IBAction contains a print statement that fires when the button is clicked. At the moment then, these two VC's only consist of a label and the newly inserted buttons. None of the buttons worked when I ran the app.
I then set one of the VC's of detailContainerView as the Initial View Controller in the Attributes Inspector and re-ran the app. Suddenly the buttons now work! If I then hook the buttons up to a segue, the segues work too!
Something seems to change when I swap out the VC's in detailContainerView. The code I am using to swap the VC's out is as follows:
func SwapOutControllers(vc: UIViewController, vcName: String){
//REMOVE OLD VC
detailPaneVCReference?.willMove(toParentViewController: nil)
detailPaneVCReference?.view.removeFromSuperview()
detailPaneVCReference?.removeFromParentViewController()
var newVc: UIViewController?
switch vcName {
case "Biography":
newVc = vc as! Biography
case "Social Media":
newVc = vc as! SocialMedia
case "News Feed":
newVc = vc as! NewsFeeds
case "Stats":
newVc = vc as! StatsAboutParliament
case "Petitions":
newVc = vc as! Petitions
default:
print("Error: No VC Found!")
}
//ADD NEW VC
ParentVC?.addChildViewController(newVc!)
let width = detailContainerView?.frame.width
let height = detailContainerView?.frame.height
newVc?.view.frame = CGRect(x: 0, y: 0, width: width!, height: height!)
detailContainerView?.addSubview((newVc?.view)!)
newVc?.didMove(toParentViewController: ParentVC)
}
detailPaneVCReference is a reference to whichever VC is currently being displayed by detailContainerView. ParentVC is the VC that contains the four container views.
The VC that is removed from the ParentVC still exists in the debugging view hierarchy after it has been removed/swapped out - could this be somehow blocking the click event from reaching the event handler?
SOLUTION!
The source of my problem has been that the references I had made to each of the view controllers that get swapped in and out of detailContainerView were incorrectly declared as weak references. I deleted "weak" (eg "weak var x: UIViewController?" --> "var x: UIViewController?") from each of the declarations and voila!, the code now works as intended!
The source of my problem has been that the references I had made to each of the view controllers that get swapped in and out of detailContainerView were incorrectly declared as weak references. I deleted "weak" (eg "weak var x: UIViewController?" --> "var x: UIViewController?") from each of the declarations and voila!, the code now works as intended!