Xamarion.iOS statusBar BackgroundColor won‘t change - ios

Since iOS13‘s darkmode i‘m working on supporting it in my Xamarin.Forms application. Everything works fine beside the StatusBar color when a NavigationBar is displayed. The usual behaviour of the Bar should be to change backgroundColor accordingly to the BGColor of the Navbar, but for some reason it always stays white.
I‘ve tried coloring it manually like this
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
statusBar.BackgroundColor = UIColor.FromRGB(61, 205, 88);
But it didn‘f change anything.
Below a screenshot of how it looks

In Xamarin.Forms Application , modifying Status Bar need to edit Info.plist in iOS solution .
First, Add UIViewControllerBasedStatusBarAppearance to file :
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
//Follow can change text color of status bar
//<key>UIStatusBarStyle</key>
//<string>UIStatusBarStyleDarkContent</string>
Second, in Xamarin.iOS project , you can modify statsbar background in AppDelegate.cs as follow :
public override void OnActivated(UIApplication uiApplication)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(13, 0))
{
// If VS has updated to the latest version , you can use StatusBarManager , else use the first line code
// UIView statusBar = new UIView(UIApplication.SharedApplication.StatusBarFrame);
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
statusBar.BackgroundColor = UIColor.Red;
statusBar.TintColor = UIColor.White;
UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
}
else
{
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = UIColor.Red;
statusBar.TintColor = UIColor.White;
UIApplication.SharedApplication.StatusBarStyle = UIStatusBarStyle.BlackOpaque;
}
}
base.OnActivated(uiApplication);
}
You can also have a look at this discussion .

Related

How to modify the background transparency of NavigationBar in Compose

I want to modify the background color transparency of NavigationBar, but the above code doesn't work.
NavigationBar(
modifier = Modifier.background(Color.Transparent),
tonalElevation = 0.dp,
containerColor = colorScheme.secondaryContainer.copy(0.7f),
contentColor = colorScheme.primary) {
}

Changing back button tint color on scroll

I am trying to find a way to change the tint color of the backBarButtonItem based on the scroll position. In the example below, the button should be yellow by default, but then at a certain threshold it should change to red.
Although using breakpoints I can see the code triggering in each block, but unfortunately backBarButtonItem never changes to red and always remains yellow. Any suggestions on why this might be the case? I'm assuming that you might not be able to change the back button in the navigation bar once it's already set.
CGFloat totalHeight = CGRectGetMaxY(self.frame);
CGFloat barHeight = CGRectGetHeight(self.frame);
CGFloat offsetHeight = (self.scrollview.contentOffset.y - self.scrollViewMinimumOffset) + totalHeight;
offsetHeight = MAX(offsetHeight, 0.0f);
offsetHeight = MIN(offsetHeight, totalHeight);
if (offsetHeight > barHeight * 1.0f) {
[self.backBarButtonItem setTintColor:[UIColor redColor]];
} else {
[self.backBarButtonItem setTintColor:[UIColor yellowColor]];
}
Let me provide the following example that can help you figure out or gain some ideas to better address the issue.
So in the storyboard (can be done programmatically), I have the following scenario:
That backBarButtonItem is actually 1stVC button in the NavigationBar.
In order to change the color of backBarButtonItem, you may implement the following code (or take a look):
import UIKit
class ViewController2: UIViewController {
var counter = 0 //any conditions you want to play with
override func viewDidLoad() {
super.viewDidLoad()
var color: UIColor = UIColor.purple //or yellow, by default
if(counter == 0){
color = UIColor.red
}
self.navigationController?.navigationBar.tintColor = color
}
}
It is done in the viewDidLoad() method of ViewController2 so that it can get configured as soon as this ViewController is opened.
Here, I just used counter variable as a simple example to create some condition based on which the color of backBarButtonItem should be changed. In your case, you have another condition.
So this is the output:

Change background color of UIToolbar in a WKWebView

I am coding my app using Xamarin, but I can translate any swift/objective-C responses.
I have a WKWebView that I using to display data with LoadHtmlString(). I want to change the background color of the UIToolbar that is displayed above the keyboard when a user focuses an input field. Right now, the button text color and background color are both white, so the buttons aren't visible.
I can change the button text color by using UIBarButtonItem.Appearance.TintColor = UIColor.White;, but this will also change the text color of the buttons in my UINavigationBar. I would rather leave the text white, and change the background color of the UIToolbar.
I can slightly modify the background color using UIToolbar.Appearance.BackgroundColor = UIColor.Red;, but this just adds a subtle tint of the color to the UIToolbar, and doesn't actually make the toolbar dark enough for the white text to be visible.
Here is my complete code for setting up my Appearance defaults:
UIColor lightColor = UIColor.White;
UIColor themeColor = UIColor.Red;
UIApplication.SharedApplication.SetStatusBarStyle(UIStatusBarStyle.LightContent, false);
UINavigationBar.Appearance.BarStyle = UIBarStyle.Black;
UINavigationBar.Appearance.BarTintColor = themeColor;
UINavigationBar.Appearance.Translucent = false;
UINavigationBar.Appearance.TintColor = lightColor;
UINavigationBar.Appearance.SetTitleTextAttributes(new UITextAttributes()
{
TextColor = lightColor
});
UIStringAttributes navBarAttributes = new UIStringAttributes();
navBarAttributes.ForegroundColor = lightColor;
UINavigationBar.Appearance.TitleTextAttributes = navBarAttributes;
UITabBar.Appearance.BarTintColor = themeColor;
UITabBar.Appearance.TintColor = lightColor;
UIToolbar.Appearance.BarTintColor = themeColor;
UIToolbar.Appearance.TintColor = lightColor;
UIBarButtonItem.Appearance.TintColor = lightColor;
UIButton.AppearanceWhenContainedIn(typeof(UINavigationBar)).TintColor = lightColor;
UIToolbar.Appearance.BackgroundColor = themeColor;
Is there a way to change the WKWebView's toolbar background color without having to retheme my entire app?
If I can't change the background color of the toolbar, I am open to changing the button text color when displayed in a WKWebView. I have tried adding the following code, but nothing seems to work.
//Using green to see if the code is working.
UIBarButtonItem.AppearanceWhenContainedIn(typeof(WKWebView)).TintColor = UIColor.Green;
UIBarButtonItem.AppearanceWhenContainedIn(typeof(MyViewController)).TintColor = UIColor.Green;
You can have a try with custom UIToolBar to change its background color by an image :
public class MyToolBar: UIToolbar
{
public override void Draw(CGRect rect)
{
base.Draw(rect);
CGContext c = UIGraphics.GetCurrentContext();
UIImage image = new UIImage("background.png");
//here set image to be background color
c.DrawImage(rect, image.CGImage);
}
}
And toolbar will only used for where you used , can not affecting other toolbar in app.
MyToolBar myToolBar = new MyToolBar();
myToolBar.Frame = new CoreGraphics.CGRect(0, 0, UIScreen.MainScreen.Bounds.Size.Width, 50);
UIBarButtonItem doneItem = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done, doneClickMethod);
List<UIBarButtonItem> list = new List<UIBarButtonItem>();
list.Add(doneItem);
myToolBar.Items = list.ToArray();
MySearchBar.InputAccessoryView = myToolBar;

Can I change the background color of the Chromecast navigation bar?

I've reviewed the custom styles available in the GoogleCast v3 SDK and unless I'm missing something I don't see a way to change the backgroundColor of the deviceChooser. See below:
Is there any way to change this gray color?
As per the documentation Google does not allow us to change the navigation bar style. So we might need to change the navigation bar appearance before pushing to the media control UI of SDK.
I tried it in didFinishLaunchingWithOptions
[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
[UINavigationBar appearance].translucent = NO;
Hope it helps :)
There are different ways you can customize the style on Chromecast SDK for iOS by using:
GCKUIStyle.sharedInstance()
My contribution for style the navigation bar:
From DOCUMENTATION I can see the following diagram
The common thing between all the "Style class" is that all of them inherit from GCKUIStyleAttributes so:
func configureChromecast() {
let gckCriteria = GCKDiscoveryCriteria(applicationID: "ABC123")
let gckCastOptions = GCKCastOptions(discoveryCriteria: gckCriteria)
GCKCastContext.setSharedInstanceWith(gckCastOptions)
GCKLogger.sharedInstance().delegate = self
// General
let textColor: UIColor = UIColor.black
let backgroundColor: UIColor = UIColor.white
GCKUIStyle.sharedInstance().castViews.backgroundColor = backgroundColor
GCKUIStyle.sharedInstance().castViews.headingTextColor = textColor
GCKUIStyle.sharedInstance().castViews.headingTextFont = UIFont.textStyleRegular
GCKUIStyle.sharedInstance().castViews.bodyTextColor = textColor
GCKUIStyle.sharedInstance().castViews.bodyTextFont = UIFont.textStyleRegular
GCKUIStyle.sharedInstance().castViews.captionTextColor = textColor
GCKUIStyle.sharedInstance().castViews.captionTextFont = UIFont.textStyleRegular
GCKUIStyle.sharedInstance().castViews.buttonTextColor = textColor
GCKUIStyle.sharedInstance().castViews.buttonTextFont = UIFont.textStyleRegular
GCKUIStyle.sharedInstance().castViews.iconTintColor = textColor
// Navigation & Toolbar
let navigationBackgroundColor: UIColor = UIColor.blue
let navigationtintColor: UIColor = UIColor.white
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.backgroundColor = navigationBackgroundColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.toolbar.backgroundColor = navigationBackgroundColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.headingTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.toolbar.headingTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.bodyTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.toolbar.bodyTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.captionTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.toolbar.captionTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.buttonTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.toolbar.buttonTextColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.iconTintColor = navigationtintColor
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.toolbar.iconTintColor = navigationtintColor
GCKUIStyle.sharedInstance().apply()
}
And the result:
The Navigation Bar style in question doesn't come from GoogleCast SDK (at least for version 4.6.1 dynamic on iOS 15) but from your own app's appearance. A way to change the background and title text color of the Navigation Bar (on GoogleCast SDK View Controllers, but your own app's as well) is to add
UINavigationBar.appearance().backgroundColor = UIColor.darkGray
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
in your AppDelegate's didFinishLaunchingWithOptions function.
But, no matter what tint color field I try setting for either UINavigationBar or UIBarButtonItem appearance, I can't get the Cancel button text color to change. I noticed that behavior in my app as well, only the Navigation Bar Buttons that have the tint set as Default in the Storyboard are affected by this global change, specifically by setting
UIBarButtonItem.appearance().tintColor = UIColor.yellow
If on the other hand you set a color in the Storyboard yourself, it will not be changed by the line of code above. This conclusion leads me to believe that is the way the Device Chooser View Controller was created, with a tint color set explicitly.
But, I see in one of the comments to the main question that someone was able to change the Cancel button color (as seen in the screenshot as well), so if anyone can share that piece of code it would be extremely appreciated.
Thanks!
EDIT
And of course only after posting this I tried pasting the entire code snippet from Reimond Hill and it worked in changing the Cancel button color, specifically this
GCKUIStyle.sharedInstance().castViews.deviceControl.connectionController.navigation.buttonTextColor = navigationtintColor
The reason why I thought this wouldn't work first time around is the fact that we are setting the navigation property of the Connection Controller, not the Device Controller (which doesn't even have this property). So I hope this will help someone else not waste time on this like I did.
You can style all GCK views using GCKUIStyle,
for example:
GCKUIStyle.sharedInstance().castViews.mediaControl.miniController.buttonTextColor = .black
GCKUIStyle.sharedInstance().apply()
in your case, the navigation can be styled with this line
connectionController.navigation.backgroundColor = UIColor.black
Check this URL for more info:
https://developers.google.com/cast/docs/ios_sender/customize_ui

Title of UIButton is not visible when UIVibrancyEffect is applied

I'm having trouble applying UIVibrancyEffect to my UIButtons in iOS today widget. I want them to like default "Edit" button in notification centre's Today section:
As you can see in the screenshot, default button is vibrant and looks much better.
I tried replacing widget's View with UIVisualEffectView like that:
UIVisualEffectView effectView = new UIVisualEffectView(UIVibrancyEffect.CreateForNotificationCenter ());
effectView.Frame = this.View.Bounds;
effectView.AutoresizingMask = this.View.AutoresizingMask;
UIView oldView = this.View;
this.View = effectView;
effectView.ContentView.AddSubview(oldView);
this.View.TintColor = UIColor.Clear;
And it appears to be working, but titles of my buttons become vibrant as well (I want them to remain black):
Is there a way to prevent button titles from becoming vibrant when UIVibrancyEffect is applied?
I should also add that I'm using Xamarin.iOS.
I seem to have a solution to my own problem. I ended up creating UIVisualEffectView with blank UIView inside and adding it behind UIButton.
This code snippet shows how to style one button:
// Create effect view
var effectView = new UIVisualEffectView(UIVibrancyEffect.CreateForNotificationCenter ());
// This color looks best for me. You can play around with it to make it look better
effectView.BackgroundColor = UIColor.FromRGBA(55, 55, 55, 100);
// Position effectView
effectView.Frame = myButton.Frame;
// Add effectView and send it to back, behind actual button
this.View.AddSubview(effectView);
this.View.SendSubviewToBack (effectView);
// Create UIView and add it to effectView's ContentView
var view = new UIView ();
view.BackgroundColor = UIColor.White;
effectView.ContentView.AddSubview(view);
view.Frame = new CGRect(0, 0, effectView.Frame.Width, effectView.Frame.Height);
// Make sure your effect view has rounded corners
effectView.Layer.MasksToBounds = true;
effectView.Layer.CornerRadius = 4.0f;
And this is how it looks:

Resources