SwiftUI - how do customise navigation bar on one screen only? - swift

At WWDC, one talk gave an example of how the ‘Reminders’ app customises the Navigation Bar on different screens. They shared how they did this:
let appearance = navigationBar.standardAppearance.copy()
navigationItem.standardAppearance = appearance
How would the actual implementation work? I’m not quite sure where that code slots in, in order to customise one navigation bar.
Any help would be super appreciated!

In viewWillAppear set this value
UINavigationBar.appearance().titleTextAttributes = [
NSFontAttributeName: UIFont(name: "MuseoSans-500", size: 19)!,
NSForegroundColorAttributeName: UIColor.black
]
and on viewWillDisappear reset all the values.

Related

How to remove strange highlighting of UINavBar when scrolling UICollectionView?

I have a child navigation view controller with a collectionView embedded in it. When I scroll it, the navigation bar appears to be dimmed a bit.
It looks like this:
Does anyone know how to remove this behavior?
I was setting my navigation bar appearance globally like that:
let appearance = UINavigationBarAppearance()
appearance.shadowImage = UIImage()
...
UINavigationBar.appearance().standardAppearance = appearance
But I finally managed to find the fix. You just have to add this:
appearance.backgroundEffect = nil
and the problem goes away.

UISearchController without use of Navigation Controller

Is it possible to use the UISearchController without the SearchBar being displayed in the Navigation Controller? I basically want the SearchBar to stay on the screen and display the TableView beneath it:
What I was trying to do is this:
I've created a UITableViewController on the storyboard and linked it to the custom NPTableViewController class. Then I did this:
let resultsTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearch") as! NPTableViewController
searchController = UISearchController(searchResultsController: resultsTable)
searchController?.searchResultsUpdater = resultsTable
let searchBar = searchController!.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search for places"
searchBar.frame.origin.y = 100.0
self.view.addSubview(searchBar)
Now when I run it, the searchBar is displayed but when I click on it the background dims, the searchBar disappears my resultsTable is also not displayed.
The solution I came up with is actually very simple. Instead of using UISearchController, I used a normal UISeachBar and UITableView. Since UISearchController doesn't do so much to help you, the work is pretty much the same if I kind of build my own SearchController.

Swift - bar button item image / set size and width

Feel like I've been wasting a lot of time on this one and would love some help. I'm trying to use a custom menu icon (20px x 20px) in my barButtonItem. The problem it that is scales to fill the bar button item and is distorted.
Normally this shouldn't be too complicated. But I'm using tabbed view controllers and not a navigation controller. The navigation bar I have dropped into the view controller is just an outlet.
Normally I could do something like this:
let image = UIImage(named: "menu_white")
let frame = CGRectMake(0, 0, image!.size.width, image!.size.height)
let button = UIButton()
button.frame = frame
button.setImage(image, forState: .Normal)
let rightMenuButton = UIBarButtonItem(customView: button)
self.navigationItem.setRightBarButtonItem(rightMenuButton, animated: true)
But this doesn't work because self.navigationItem doesn't actually refer to anything in the view controller.
I need to somehow get the image into the outlet programmatically but setRightBarButtonItem is not a method of UINavigationBar.
#IBOutlet weak var navBar: UINavigationBar!
Would really appreciate some help if anyone's got some ideas.
The way a "loose" navigation bar works is that you configure a UINavigationItem and set an array containing that to be the navigation bar's items, as in this example code (from my book):
let ni = UINavigationItem(title: "Tinker")
let b = UIBarButtonItem(title: "Evers", style: .Plain, target: self, action: "pushNext:")
ni.rightBarButtonItem = b
self.navbar.items = [ni]
So, you could presumably do something like that.
The self.navigationItem thing is merely a way of causing that to be done automatically in the special situation where you're using a UINavigationController. But you say that you're not doing that. (Of course, you could do that; there's no law that says you have to use a navigation controller to do any actual navigation. I often use a navigation controller just to get the navigation bar, because it's a good place to put things like little menu buttons.)

UITabBarController customisation page top bar colour swift

How do I change the colour of the top bar of the customisation page of the more view controller. Please see the linked image. Sorry I can't post image here because of my low reputation.
Screenshot Image
Edited with more info:
I have managed to change the background color using the following code. But cant change the color of the top bar.
func tabBarController(tabBarController: UITabBarController, willBeginCustomizingViewControllers viewControllers: [AnyObject]) {
var editView : UIView = tabBarController.view.subviews[1] as! UIView
editView.backgroundColor = UIColor.blackColor()
}
Basically 2 Ways you can achieve this if you are using Global Unique colour in the app for All navigation bar
Use this Solution on App Launch:
UINavigationBar.appearance().barTintColor = UIColor.redColor()
Or if you want to change the Color of More navigationcontroller only then
Use this Solution by getting the Tabbar Reference :
self.tabBarController?.moreNavigationController.navigationBar.barTintColor = UIColor.greenColor()
You can use second solution in the First viewcontroller of the Tab, because that contains your tabbar reference

Trying to change mail compose navigation bar text colour

I am trying to change the colour of the navigation bar text for the email compose view which I previously had working, however since I have changed the size and font of the navigation bar text throughout the app, this has broken my code.
I have this code in the view controller that segues to the view controller that contains the email button:
var attributes = [NSForegroundColorAttributeName: UIColor.whiteColor(),NSFontAttributeName: UIFont(name: "Avenir", size: 24)]
self.navigationController?.navigationBar.titleTextAttributes = attributes
This is the code I was previously using to change the navigation bar colour to white:
mc.navigationBar.tintColor = UIColor.whiteColor()
I have this code in viewDidLoad in one of my view controllers, but it doesn't affect the mail compose view controller:
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
Any ideas?
mailCnt.navigationBar.tintColor = UIColor.whiteColor
should work