Swift - Adding a navigation bar that overrides the existing one - swift

I have multiple VC's embedded in one NavigationController.
I have one VC, lets name it VCNotTransparent, that I want the bar to be not transparent, and on other VC's I want it to be transparent.
So in the main VC, I added these lines for making the bar transparent:
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
So now all of my bars are transparent in the app.
How can I make VCNotTransparent not transparent without it changing all of the other VC's? one solution I thought of is to add a new navigation bar only in VCNotTransparent, but I do not know how to do that.
EDIT
I also tried embedding VCNotTransparent in its own NavigationController, which works almost, but the issue is that I have navigation from it to some other VC's and they become not transparent as well, since they are sub navigation of the VCNotTransparent.

Handle this by enum -
Do below within your MainVC -
public enum NavigationType: Int {
case transparent = 1
case notTransparent = 2
}
var currentNavigationType: NavigationType?
override func viewDidLoad() {
super.viewDidLoad()
self.currentNavigationType = .transparent // default
self.setupNavigationControllerStyle()
}
func setupNavigationControllerStyle (){
switch self.currentNavigationType! {
case .transparent:
//do code here for transparent
case .notTransparent:
//do code here for not transparent
default:
break
}
}
default it will show transparent bar. in which controller you don't want transparent bar just update the currentNavigationType property from there like below -
class VCNotTransparent: MainVC {
override func viewWillAppear(_ animated: Bool) {
self.currentNavigationType = .notTransparent
super.viewWillAppear(animated)
}
}

Related

Presenting UISearchController programmatically

I am presenting a UISearchController programmatically, without adding it to the navigationItem. The Calendar app does something similar.
Without a presentation context, the search bar appears correctly, but persists after pushing another view controller.
This is expected, so we need to set definesPresentationContext on the list view controller... But that causes the search bar to render incorrectly.
Here's the code for context:
private lazy var searchController: UISearchController! = {
let searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
// If this is set to true, the search bar animates correctly, but that's
// not the effect I'm after. See the next video.
searchController.hidesNavigationBarDuringPresentation = false
return searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
definesPresentationContext = true
searchButton.rx.tap.subscribe(onNext: { [unowned self] in
present(searchController, animated: true)
}).disposed(by: disposeBag)
}
Setting hidesNavigationBarDuringPresentation kind of fixes it, but we lose the tab bar, and the whole thing just looks bad.
I tried this solution (Unable to present a UISearchController), but it didn't help.
Any suggestions?
UPDATE: The issue is, more specifically, that the search bar appears behind the translucent navigation bar. Making the nav bar solid ( navigationController?.navigationBar.isTranslucent = false) makes the search bar appear under the nav bar.
I have the same problem not been able to solve this either. It seems like the problem is that either
a) the searchcontroller is presented at the very top of the viewcontroller stack, even above the navigation controller, so that it stays active into the next viewcontroller push. or,
b) the searchcontroller is presented underneath the navigationcontroller so that it remains covered by the navigation bar
One idea: don't embed the viewcontroller which is presenting the searchcontroller in a navigation controller. instead, just create a UIView which looks like a navigation bar a the top. would this be an inappropriate solution?
I haven't found a solution to the original problem, but I found a workaround: intercept navigation events, and manually dismiss the search controller.
override func viewDidLoad() {
...
// This makes the search bar appear behind the nav bar
// definesPresentationContext = true
navigationController?.delegate = self
}
extension JobListViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController,
willShow viewController: UIViewController, animated: Bool) {
// `animated` is false because, for some reason, the dismissal animation doesn't start
// until the transition has completed, when we've already arrived at the new controller
searchController.dismiss(animated: false, completion: nil)
}
}
I found that presenting the search controller over the navigation bar can be achieved by calling present(_:animated:completion:) on the navigation controller itself rather than the navigation controller's child.
So in your view controller you can do
navigationController?.present(searchController, animated: true)
And this will behave like the search button in the Apple's Calendar app.
Update
Regarding dismissing the search controller before pushing a new controller to the navigation stack, you can do this manually depending on how the push is done.
All the bellow will animate dismissing the search controller before the push happens. Note that I disable user interaction until the dismiss animation completes to prevent pushing the same view controller multiple times.
UIStoryboardSegue
Add this override to your view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if navigationController?.presentedViewController == searchController {
view.isUserInteractionEnabled = false
searchController.dismiss(animated: true) {
self.view.isUserInteractionEnabled = true
}
}
}
IBAction (Programmatically)
Just dismiss the search controller before pushing view controllers to the navigation stack:
#IBAction func showSecondTapped(_ sender: UIButton) {
// Dismiss the search controller first.
view.isUserInteractionEnabled = false
searchController.dismiss(animated: true) {
self.view.isUserInteractionEnabled = true
}
// Build and push the detail view controller.
if let secondViewController = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") {
navigationController?.pushViewController(secondViewController, animated: true)
}
}
Handling pop gesture
If the view controller that is presenting the search controller is not the root of your navigation controller, the user might be able to use the interactive pop gesture, which will also keep the search controller presented after the pop. You can handle this by making your view controller the delegate for the search controller, conform to UISearchControllerDelegate and adding the following code:
extension ViewController: UISearchControllerDelegate {
func willPresentSearchController(_ searchController: UISearchController) {
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
func willDismissSearchController(_ searchController: UISearchController) {
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}
}

iOS non blocking status bar

I'm trying to place an image behind the status bar.
I'm able to turn it transparent but It's still blocking the image from appearing behind it.
Does anybody know how to make the status bar apart of the editable screen and/or safe area? I don't want to delete it, just want to put stuff behind it.
Here's what IB looks like
Code
override func viewWillAppear(_ animated: Bool) {
setNeedsStatusBarAppearanceUpdate()
}
override func viewDidLoad() {
super.viewDidLoad()
venueInfoTableView.dataSource = self
venueInfoTableView.delegate = self
// Do any additional setup after loading the view, typically from a nib.
venueInfoTableView.separatorStyle = .none
}
override var preferredStatusBarStyle : UIStatusBarStyle {
return UIStatusBarStyle.lightContent
//return UIStatusBarStyle.default // Make dark again
}
And here's the result
You should disable automaticallyAdjustsScrollViewInsets
if #available(iOS 11.0, *) {
self.venueInfoTableView.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
contentInsetAdjustmentBehavior This property specifies how the safe
area insets are used to modify the content area of the scroll view.
The default value of this property is automatic.

navigationbar missing after using UISearchController

i have a UICollectionViewController where i am showing list of task which is working fine, recently i tried to implement a UISearchBar for my TaskController after implementing that, when i try to launch any new viewcontroller by clicking on row inside my TaskController the newly launched view controller does not have UINavigationBar so i cant move back to my task list again. see following TaskController with task list:
Image
in above screen shot there is a star icon when user click on that, I launch following view controller which have a navigation bar(note: I have click directly without filtering records thats why i can see the navigation bar here.). UIViewController with UINavigationBar
Image
this is what i get when i click on star icon after filtering data with search bar.
navigation bar gone missing here
so i can not go back to task list controller also when i change a tab from below and come back the view controller got destroyed and i get a black screen with tab bar.
following code i have used to implement search bar which have the problem please help me to figure it out.
let taskSearchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
//set taskSearchController
taskSearchController.searchResultsUpdater = self
taskSearchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = taskSearchController
getTaskList(){
}
}
following method gives the filtered data from tasklist
func updateSearchResults(for searchController: UISearchController) {
guard let searchText = searchController.searchBar.text, !searchText.isEmpty else{
self.taskList = self.originalTaskist
collectionView?.reloadData()
return
}
taskList = originalTaskist.filter({ task -> Bool in
return task.name!.lowercased().contains(searchText.lowercased())
})
collectionView?.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
// This prevents the search bar to make trouble on pushed view controllers
definesPresentationContext = true
//...
}
Put definesPresentationContext = true inside of your View Controller that shows the search bar (the UICollectionViewController in your case.
Unfortunately, the documentation doesn't explain very well why this is working. This blog post explains it a little better.

NSTabViewController add NSToolbarItems

I'd like to use the NSTabViewController for switching through 6 different Tabs with the toolbar style.
All tabs have in common that they show different aspects of a Customer entity.
Now I want to add aditional NSToolbarItems to the toolbar of the NSTabViewController? But I haven't found a way to access the toolbar.
I also would like to add Space between the ToolbarItems.
Is there a way to do so?
Or how can I add my ViewController from the Storyboard to a NSTabView without using NSTabViewController?
Regards
Oliver
In the meantime I've tried another approach that I thought was more promising but lead to another strange behaviour:
I've created a new NSViewController and put a NSTabView inside. In order to load my already existing ViewControllers I used this
override func viewDidLoad() {
super.viewDidLoad()
let customerController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("CustomerVCID")) as! CustomerViewController
let servicesController = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier("ServicesVCID")) as! ServicesController
customerController.customer = self.customer
servicesController.customer = self.customer
self.tabView.tabViewItems[0].view = customerController.view
self.tabView.tabViewItems[1].view = servicesController.view
}
That indeed worked, but now all my NSButtons that have actions will cause my application to crash.
There is only one toolbar per window. So your NSTabViewController shares it.
Select toolbar mode of NSTabViewController
Override NSWindowController and add your items
Example:
override func windowDidLoad() {
super.windowDidLoad()
window?.toolbar?.insertItem(withItemIdentifier: .print, at: 0)
}
You can always access your toolbar via following path view->window->toolbar
Your only issue is that there is one delegate per NSToolbar. Which means you have to create your custom NSToolbarItem inside NSTabViewController delegate.
override func toolbar(_ toolbar: NSToolbar, itemForItemIdentifier itemIdentifier: NSToolbarItem.Identifier, willBeInsertedIntoToolbar flag: Bool) -> NSToolbarItem? {
if itemIdentifier == .export {
return ExportToolbarItem.new()
} else {
return super.toolbar(toolbar, itemForItemIdentifier: itemIdentifier, willBeInsertedIntoToolbar: flag)
}
}
Remember your are required to call super. This is because underlying method wants to create bindings to view controller.
In case you need actionable buttons in toolbar just add them without calling super.

(Swift) Tab Bar Item: Custom Selected Image with Rendering asOriginal is Not Showing

First of all, I'm a newbie to Swift so apologies if I'm missing something obvious or using the wrong terminology.
Objective: set tab bar item selected image to custom image.
The following setup works (selected item is custom image):
| UITabBarController | => | UIViewController | (setup w/ storyboard)
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let customSelectedImage = UIImage (named: "selected-image")?.withRenderingMode(.alwaysOriginal)
self.tabBarItem.selectedImage = customSelectedImage
}
}
But this setup doesn't work (selected item has default blue tint):
| UITabBarController | => | UINavigationController | => | UIViewController | (setup w/ storyboard - see here)
Similar code to above but added (programmatically) UICollectionView subview to UIViewController.
class MyViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
let customSelectedImage = UIImage (named: "selected-image")?.withRenderingMode(.alwaysOriginal)
self.tabBarItem.selectedImage = customSelectedImage
...
//Some UICollectionView related code
...
}
}
Some things that may be helpful:
In debug session (see print screen) => View UI hierarchy: the selected item (marked as of class UITabBarSwappableImageView) has the correct custom image but the tint is default blue. I tried with different custom images and looks as if they're hidden by another (default ?) view...
If I change the UITabBar.appearance().tintColor = UIColor.red in AppDelegate.swift application(... didFinishLaunchingWithOptions ...) function then the selected item has a red (vs blue) tint.
What's happening?
I extend UITabBarController:
extension UITabBarController {
func updateTabBarItem(tab: Int, image: UIImage?) {
guard let tabItems = tabBar.items, tab < tabItems.count && tab >= 0
else { return }
let tabItem = tabItems[tab]
tabItem.image = image?.withRenderingMode(.alwaysOriginal)
tabItem.selectedImage = tabItem.image
}
}
This will help to access the tabBar.items without loading any view controllers (except the first view controller of the tab 0).
class MyViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tabBarController?.updateTabBarItem(tab: 1, image: UIImage(named: "selected-image")) // update the second tab's image here (just for example)
}
}
For example, if you want to change the tab 2 selected image, make a break point on viewDidLoad: on the second view controller, you will find the break point doesn't hit, that's why the tab item's image wouldn't be updated.