How to set statusBar color on iPhoneX? - iphone-x

I use the navigationController, and i hide the navigationBar, how to set statusBar color on iPhoneX?
I do not want to set self.view.backgroundColor
iPhoneX notch color

UIView *statusBar = (UIView *)[[UIApplication sharedApplication] valueForKey:#"statusBar"];
statusBar.backgroundColor = [UIColor greenColor];

Here is a hacky trick to change/set background color for status bar during application launch or during viewDidLoad of your view controller.
extension UIApplication {
var statusBarView: UIView? {
return value(forKey: "statusBar") as? UIView
}
}
// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
return true
}
}
or
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
}
}
Here is result:

Swift 4
func setStatusBarBackgroundColor(_ color: UIColor) {
guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
statusBar.backgroundColor = color
}

In Interface Builder, I simply added a view at 0x0, 44px high and the same width as the top-level view, then set this view to the desired background color. This changed the background color of the status bar without affecting the view background color.
In my case, I needed something like this because the top half of my screen is one color and the bottom half is the other. I didn't see another way to make the status bar match the top color and the home button area match the bottom color.
This could easily be done programmatically if the extra view gets in the way on devices other than the iPhone X. In my case it doesn't cause problems on other devices.

Related

Grouping textbox and label together

I have a lookup view on my xamarin.iOS app where the user puts their subdomain in and I want the textbox to be to the left of the label that has the parent domain, the image below shows what I basically want to achieve:
I was easily able to achieve this on android using drawables but my iOS skills are not as sharp so I'm lost on how I do this on iOS.
in iOS ,you can implment it in code.
in xxxViewController
public override void ViewDidLoad()
{
base.ViewDidLoad();
UIView backgroundView = new UIView(new CGRect(10,50,View.Bounds.Width-20,30));
UILabel domainLab = new UILabel()
{
Text = " https://www.example.com ",
Font = UIFont.SystemFontOfSize(12),
TextColor = UIColor.Gray ,
BackgroundColor=UIColor.LightGray,
Frame=new CGRect(0,0,150,30)
};
UITextField textField = new UITextField()
{
Frame=new CGRect(150,0,View.Bounds.Width-170,30),
};
textField.Layer.MasksToBounds = true;
textField.Layer.BorderColor = UIColor.LightGray.CGColor;
textField.Layer.BorderWidth = (System.nfloat)0.5;
backgroundView.AddSubview(domainLab);
backgroundView.AddSubview(textField);
View.AddSubview(backgroundView);
// Perform any additional setup after loading the view, typically from a nib.
}

Xamarin iOS Transparent Navigation bar

Need to have transparent navigation bar
https://developer.xamarin.com/recipes/ios/content_controls/navigation_controller/change_the_nav_bar_transparency/
tried with this but not working.
Any Suggestion ?
Although, your question is not fully descriptive and not sure what exactly you want to achieve by saying "Not Working" from the given link.
Write following code in respective override methods of the ViewController in which you want to achieve the Transperent NavigationBar for that specific ViewController only.
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
if (NavigationController != null)
{
NavigationController.NavigationBar.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = new UIImage();
NavigationController.NavigationBar.Translucent = true;
NavigationController.View.BackgroundColor = UIColor.Clear;
NavigationController.NavigationBar.BackgroundColor = UIColor.Clear;
}
}
public override void ViewWillDisappear(bool animated)
{
base.ViewWillDisappear(animated);
if (NavigationController != null)
{
NavigationController.NavigationBar.SetBackgroundImage(null, UIBarMetrics.Default);
NavigationController.NavigationBar.ShadowImage = null;
NavigationController.NavigationBar.BarTintColor = UIColor.Red; //Provide your specific color here
}
}
If you want to set this Globally, Try it in AppDelegate's FinishedLaunching method :
UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
UINavigationBar.Appearance.ShadowImage = new UIImage();
UINavigationBar.Appearance.BackgroundColor = UIColor.Clear;
UINavigationBar.Appearance.Translucent = true;
Hope this helps!.
A single navigation bar is used for a particular navigation controller in iOS. So if you want to make it transparent for a single VC then you have to make it transparent when you navigate to that VC. And then change it back when you coming back from that VC in viewWillDisappear (or viewDidDisappear) methods.

iOS 9 issues - GameCenter

Since iOS 9 errors are appearing in the console when opening the Game Center View Controller. I have created a bare bones sample project to help diagnose and isolate the issue. My bare bones View Controller contains a UIButton to open the Game Center, and my code is the following:
import UIKit
import GameKit
class ViewController: UIViewController, GKGameCenterControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if let viewController = viewController {
self.presentViewController(viewController, animated: true, completion: nil)
}else{
print("(GameCenter) Player authenticated: \(GKLocalPlayer.localPlayer().authenticated)")
}
}
}
#IBAction func openGameCenter(sender: AnyObject) {
let gameCenter = GKGameCenterViewController()
gameCenter.gameCenterDelegate = self
self.presentViewController(gameCenter, animated: true, completion: nil)
}
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
}
When presenting the Game Center View Controller, I see the following errors in the console:
2015-09-21 11:03:41.530 GameCenterTest[1552:415921] - changing property masksToBounds in transform-only
layer, will have no effect 2015-09-21 11:03:41.531
GameCenterTest[1552:415921] - changing
property masksToBounds in transform-only layer, will have no effect
2015-09-21 11:03:41.532 GameCenterTest[1552:415921] - changing property masksToBounds in transform-only
layer, will have no effect
When tapping on Done and closing the Game Center View Cotroller, I see the following error in the console:
2015-09-21 11:03:52.263 GameCenterTest[1552:416142] plugin
com.apple.GameCenterUI.GameCenterDashboardExtension invalidated

Setting a custom font for Monotouch-dialog elements

Is there a way to set the fonts when a Monotouch Dialog class has been instansiated?
[Section("This is the header")]
This will render with the default blue text with drop shadow, but I can't find where that font is being set. Is there a way to overwrite which font and color it uses?
I found a solution for those looking to replace ALL section headers in the entire solution. In MonoTouch.Dialog, there is a class named DialogViewController which is used when creating views with the reflection API. And in here, there's a method called GetViewForHeader(). Instead of sending back just the normal section.HeaderView, you can create a custom label and send that back.
public override UIView GetViewForHeader (UITableView tableView, int sectionIdx)
{
var section = Root.Sections [sectionIdx];
if (!string.IsNullOrEmpty(section.Caption))
{
var label = new UILabel();
label.BackgroundColor = UIColor.FromRGB(89, 41, 17);
label.TextColor = UIColor.FromRGB(255, 206, 52);
label.ShadowColor = UIColor.Black;
label.ShadowOffset = new SizeF(0, 1f);
label.Font = UIFont.FromName("TitlingGothicFB Cond", 20);
label.Text = section.Caption;
return label;
}
return section.HeaderView;
}
public override float GetHeightForHeader (UITableView tableView, int sectionIdx)
{
if (!string.IsNullOrEmpty(section.Caption))
return 40f;
return -1;
}
Remember to set the height, either manually or by getting the height from the label. You can also create a custom UIView, but a label was sufficient for me.
When you use the Section like that you will use the UITableView standard rendering.
The only way to change that is to use the Element API instead of the reflection API, and provide a UIView where you draw the contents of the data yourself.

How can I animate a View to fly in from the bottom of the screen?

I'm trying to figure out how to have a view. Let's call it ThirdView. It should slide up from the bottom of the screen when a user clicks a particular button on SecondView.
You'll want to create the ThirdView in your SecondView and present it as a modal view, passing in the secondView in the constructor. This will be the easiest way of animating it in the way you would like.
var thirdView = new ThirdView(secondView);
this.PresentModalViewController(thirdView, true);
In your third view, you'll want to call the passed-in SecondView and call:
secondView.DismissModalViewControllerAnimated(true);
Here is a complete working example. It is a tad simpler than in chrisntr's answer...though the above example is what I used to figure everything out.
The coolest thing about this method is that for an artistic custom UI (like the one I am building for a game), there is no off-the-shelf UI elements like the TabBar, Navigation bars, etc. The most creative applications don't use standard UI stuff.
In your main.cs file, in your finishedlaunching block:
ViewController myUIV = new ViewController();
window.AddSubview(myUIV.View);
window.MakeKeyAndVisble();
And then in a new code file add this code:
using System;
using System.Drawing;
using MonoTouch.UIKit;
namespace AnimationTest
{
public class ViewController : UIViewController
{
UIButton uib = new UIButton(new RectangleF(100, 100, 40, 40));
public override void ViewDidLoad()
{
Console.WriteLine("UI1");
this.View.BackgroundColor = UIColor.Blue;
uib.BackgroundColor = UIColor.White;
uib.TouchUpInside += delegate {
Console.WriteLine("Hey!");
var vc2 = new SecondController();
PresentModalViewController(vc2, true);
};
this.View.AddSubview(uib);
base.ViewDidLoad();
}
}
public class SecondController : UIViewController
{
UIButton uib = new UIButton(new RectangleF(100, 100, 40, 40));
public override void ViewDidLoad()
{
this.View.BackgroundColor = UIColor.White;
uib.BackgroundColor = UIColor.Red;
uib.TouchUpInside += delegate {
this.DismissModalViewControllerAnimated(true);
};
this.View.AddSubview(uib);
base.ViewDidLoad();
}
}

Resources