Swift: Refresh Tab Bar Background Colour Dynamically - ios

I've created a config manager to update UI settings without restarting. Most settings you can just force a redraw however it appears to be more limited with the tab bar.
I update the tab bar within my UITabBarController with the following when I receive a config change notification (note: this could be anytime after viewDidLoad):
self.tabBar.backgroundColor = tabBarBackgroundColour
This does update the colour but only after about 5-10 seconds.
I've tried various ways to force an update using setNeedsLayout and setNeedsDisplay on both the tab bar and the view but none of them work.
self.tabBar.setNeedsLayout()
self.tabBar.setNeedsDisplay()
or
self.view.setNeedsLayout()
self.view.setNeedsDisplay()
I've also tried updating the individual tab bar items as I see other people have had similar issues when updating images but to no avail.

Make sure you are updating the background color on the main thread.
DispatchQueue.main.async { [weak self] in
self?.tabBar.backgroundColor = tabBarBackgroundColour
}

Related

How do you properly dim tint colors when tintAdjustmentMode is active and using the new contentConfigurations?

Say I want to mimic this "Shut Down" blue-tinted button in the iOS Settings app using the new (as of iOS 14) cell content configuration options:
How would I do this in a way that would also allow it to be properly dimmed if a user presented, say, an alert controller over top? For the unaware in this case it "dims" all the interactive/tinted views like this one to a grey color. Here's an example where an alert controller is shown over a grid of UIImageView buttons with a normally blue tint where you can see they're grey due to the action sheet:
But with cell content configurations, at least the default one, the only option I get for changing text color is via textProperties.color = ..., and since this isn't technically a "tint" (and there's no tint option I can see) it remains blue even when other views properly dim.
How do I fix this?
I tried reading the value of tintAdjustmentMode in the updateConfiguration method of the cell subclass, but it's not called for tintAdjustmentMode changes. I then tried calling setNeedsUpdateConfiguration() manually when tintColorDidChange() is called on the subclass, but that causes a brief delay in the color changing which looks bad/out of sync with other views.
Is there a way to do this that I'm missing?

UIRefreshControl is flickering and transitions are abrupt

I'd like to set up a UIRefreshControl in my apps' main table view controller to trigger a refresh function to (you probably guessed it by now) refresh it.
Adding it isn't the problem, I'm able to set it up through the storyboard (by enabling refreshing) or in the controller (by using self.refreshControl = UIRefreshControl()).
When it gets triggered by the user it starts refreshing normally, but when I trigger it through self.refreshControl.beginRefreshing() the control isn't displayed in the navigation bar, until I scroll down (like you would normally trigger it)
But that's not even the weirdest part. When it's finally visible to the user there is a constant flicker to it and after a while it's not visible at all.
For the self.refreshControl.endRefreshing() method, the animation is really abrupt any choppy and sometimes the navigation bar doesn't scroll up again (leaving an empty space at the top)
Here is a gif, that should summarise my problem
I use swift 4.2, the application was tested on different simulators (running iOS 12.1) and my iPhone X (running iOS 12.1.2) and the release target is iOS 10.0.
I've already done research on my problem, but no one seems to have the exact issue (at least the flickering) or the solutions don't work for me.
This is how my view controller is set up:
class MyViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.refreshControl = UIRefreshControl()
self.refreshControl?.addTarget(self, action: #selector(reload), for: .valueChanged)
self.refreshControl.beginRefreshing()
}
#objc func reload() {
DispatchQueue(label: "update").async {
sleep(10)
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
}
}
}
}
Thanks for reading, I really hope someone can help me.
Update
I just discovered that the issue only occurs with a non-translucent navigation bar with large title, so picking a translucent navigation bar instead of the opaque is a solution (sadly not one I would like to live with). Maybe someone has an approach to having a functional refresh control with an opaque navigation bar.
Additionally I submitted a bug report to Apple, because something like that should not happen in the first place, I'll update this post when they get back to me.

iOS 11 SearchDisplayController shows black status bar

I know SearchDisplayController is deprecated, but Storyboard still supports it, and it is an easy way to present tableViewController on top of your view controller. I have been using it, and I would still prefer to use that. And in iOS 11, when I run my app, the status bar of the SearchDisplayController. after the search bar is focused, is pitch BLACK. Does anyone know how to solve this bug? Also if you realize, the margins of the searcher is off. I am using the default iOS 11 searchbar. Below is attached screenshot:
Have you tried to set the extendedLayoutIncludesOpaqueBars property to true?
searchDisplayController.extendedLayoutIncludesOpaqueBars = true
This is not the best solution actually, but It works to change status bar color.
if let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView {
statusBar.backgroundColor = //YOUR COLOR HERE
}
Keep in mind this will affect the whole app. If you need to change some views only then save the previous color to restore it.
Regards.

Long delay in displaying screen swift 2.2

My application reads data from MySql server and displays it in a tableview. At the start of the call to the server the application covers the tableView with a view (named "pdView") that has background colour of Light Gray. That view is part of the scene which the tableView is also part of (designed in IB), but is in a hidden state, until just before the call to the server where it becomes visible using
pdView.hidden = false
After the app gets the data and fills the tableview, using
dispatch_async(dispatch_get_main_queue(), { () -> Void in
tableview.reloadData()
})
one can see the data displayed in the tableView, under the gray display.
I then try to hide back the view using
pdView.hidden = true
but it takes the app about 44 seconds for the gray color to disappear. During that time my app behaves normally and I can scroll the table up and down.
I tried putting the code to hide the view inside dispatch_async() but to no avail.
What needs to be done to fix this problem so that the pdView disappear right after it is back to the hidden state?
ALL your UI code needs to be from the main thread. That includes things like changing the state of a view's hidden flag.
Do you have that code wrapped in a call to
dispatch_async(dispatch_get_main_queue()
as well?
It sounds like you are trying to update the UI from a separate thread. You will need to give your pdView.hidden = true some context. You can try
dispatch_async(dispatch_get_main_queue(), { () -> Void in
tableview.reloadData()
self.pdView.hidden = true
})
Note the self in self.pdView.hidden..
Hope that helps.

How to fade in/out navigationBar on iOS 9?

Built-in Photo application fades in/out navigationBar when you tap on an image . This way Photo app allows to see it full screen.
How does it do this (fade efect)?
As I understand navigationController?.navigationBar.alpha doesn't work anymore (so you can't animate it this way).
Sharing all my finding.
Complain mode on
Frankly, I feel half pissed/like a dummy that I had to fight a good day to implement simple thing existing in Apple app.
Complain mode off
First of all here is some context. I am working with navigationBar which are provided by navigationController (vs just standalone bars which are manually dropped in your view)
There are several approaches which I found. I will mention all of them (even if I had no success using them)
1) Animate change of alpha of navigationBar
UIView.animateWithDuration(0.1, animations: {
navigationController?.navigationBar.alpha = 0
}, completion: nil)
#rmaddy mention here that it works for him. However, I believe he has a standalone bar (vs a bar managed by navigationController).
I used a tool Reveal to check UI hierarchy and found couple of things.
- There is a navigationBar which is hidden (and navigationController?.navigationBar is referencing it). So you can change alpha to your hearts joy, but these changes won't be visible.
There is however another navigationBar . I assume it's referenced in some private members of navigationController (let's call it private navigationBar). It's visible and that's what is displayed at the top of your view.
2) Use setNavigationBarHidden:animated:
This is a standard way to hide/show navigation bar. It's animated different way (it slides/up and down). However, if it's ok for you, just go with this is, because it's simple and clean.
navigationController?.setNavigationBarHidden(true, animated: true)
Additionally you can wrap it in UIView.beginAnimations, UIView.commitAnimations to animate it together with some other stuff (to make it smoother)
3) Animate change of alpha of private navigation bar.
This worked for me:
let privateNavigationBar = self.superview?.superview?.superview?.superview?.superview?.superview?.subviews[1]
UIView.animateWithDuration(0.1, animations: {
privateNavigationBar.alpha = 0
}, completion: nil)
I am going way up through the hierarchy to get a view which contains private navigationBar (which is second subview for that view).
However, this approach has multiple downsides:
I believe # of superviews? depends on your app hierarchy (vs you are using split view and so on). I think you can generalize or may be you just walk the whole hierarchy to find non hidden UINavigationBar to solve this.
I have a feeling that Apple may frown at this (your app be not accepted to AppStore)
4) Make navigationBar transparent and set background image to be transparent and change alpha channel on it.
I can't find where I read about this idea. There was couple of mentioning.
There is Apple example app which shows how to customize NavigationBar, including making it transparent.
It's interesting that this example app works for me (the navigation bar is transparent in it). However, when I tried this code in my app it didn't work (I still didn't figured out what is going on with this). As usual there are bunch of variables (may be something in Info.plist, also they subclass NavigationController, also may be something in view hierarchy)
5) Adding standalone navigationBar
You can hide a bar provided by navigationController. Add your own to the UIView, wire it to #IBOutlet and use alpha animation on it (most likely that's what #rmaddy was referring too).
I checked and this is work.
This approach is used in this tutorial.
However, it has a downside:
I believe it won't handle well rotation, increase of statusbar height while call or GPS
Each time when I see a code like this (written in the article) I know that there will be problems with resizing: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64.0)
You can potentially replace it with constrains. I went this route, but stumble upon some issues.
6) Other methods
I saw two more methods. I don't know whether they will work or what will be downsides:
One of them in this question: How to hide/show status bar and navigation bar by fading in/out at the same time like the Photos app in iOS 7?
And this answer: https://stackoverflow.com/a/18063898/422080

Resources