Monotouch - Popups over everything - xamarin.ios

On iPhone, in Xcode, I can show a popup view which overlays everything, including the Tab Bar, etc, by using code like this -
[[[[UIApplication sharedApplication] delegate] window] addSubview:mySpecialView];
I'm trying to do the same in MonoTouch, and the code I'm using is this -
UIApplication.SharedApplication.Delegate.Window.AddSubview(mySpecialView);
...but this crashes. Does anyone have any idea what I'm doing wrong?
Thanks for any help.

You did not say how it crashed - but I assume you're having a ModelNotImplementedException while using the Window property since it's not implemented by default (and is meant for storyboard).
You can either implement it to return the window field of the (autogenerated) AppDelegate (AppDelegate.cs file) or expose the same variable as a (static) field.
E.g. the default generated code
UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = new UINavigationController ();
window.MakeKeyAndVisible ();
return true;
}
would become:
static UIWindow window;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = new UINavigationController ();
window.MakeKeyAndVisible ();
return true;
}
static public UIWindow Window {
get { return window; }
}

Related

Hiding the Navigation Bar in Xamarin Project using Mvvmcross

I'm using mvvmcross and implementing the view's interface in code behind. I would like to hide the navigation bar but I have not found a solution yet.
I tried
NavigationController.SetNavigationBarHidden(true, false);
and
NavigationController.NavigationBarHidden = true;
in different methods (ViewDidAppear and ViewWillAppear) but they don't have an impact on the UI.
Maybe someone could give me a hint. :-)
#Edit: Some more information:
My AppDelegate.cs
[Register("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate
{
UIWindow _window;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
_window = new UIWindow(UIScreen.MainScreen.Bounds);
var setup = new Setup(this, _window);
setup.Initialize();
var startup = Mvx.Resolve<IMvxAppStart>();
startup.Start();
_window.MakeKeyAndVisible();
return true;
}
}
Additionally I'm using a BaseView class which inherits from MvxViewController.
Okay, found the solution by myself:
Just paste the following code into the ViewDidLoad method in your MvxViewController class( for example FirstView.cs in many mvvmcross tutorials):
var navController = base.NavigationController;
navController.NavigationBarHidden = true;
I know it is a +6 years old question but came across finding a solution for this using MVVMCross and found out that using this into the xaml of your view should be enough: <NavigationPage.HasNavigationBar>False</NavigationPage.HasNavigationBar>
It should apply for both Xamarin Android and iOS.
This will kill it, let me know if you have questions.
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
// class-level declarations
UIWindow window;
MyViewController viewController;
MainViewController mainViewController;
UINavigationController navController;
public UINavigationController NavController { get { return navController; }}
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
var navController = new UINavigationController();
navController.SetNavigationBarHidden (true, false);
window = new UIWindow (UIScreen.MainScreen.Bounds);
viewController = new MyViewController();
app.SetStatusBarStyle (UIStatusBarStyle.LightContent, true);
navController.PushViewController(viewController, false);
window.RootViewController = navController;
window.MakeKeyAndVisible ();
return true;
}
}
}
The default presenter uses a UINavigationController for the RootController on the window; so you can manipulate the navigation bar globally in the AppDelegate by grabbing it off the window and casting:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
window = new UIWindow(UIScreen.MainScreen.Bounds);
new Setup(this, window).Initialize();
Mvx.Resolve<IMvxAppStart>().Start();
var navigationBar = ((UINavigationController)window.RootViewController).NavigationBar;
navigationBar.BackgroundColor = UIColor.Black;
navigationBar.BarTintColor = UIColor.Black;
navigationBar.TintColor = UIColor.White;
window.MakeKeyAndVisible();
return true;
}

Playing YouTube video in iPhone only app - loss of controls

The code below is used to put a small WebView on a View so that the user can tap it and the video opens in full screen mode and plays. All that works, but after 4 seconds of play the controls disappear and will not reappear (tapping, rotating...). Once the video finishes, the controls reappear and the 'Done' button becomes available. However once the WebView is disposed of and a new view loaded, that new view is unresponsive for up to 6 minutes.
[Preserve (AllMembers=true)]
public class YouTubeViewer : UIWebView
{
public static AppDelegate appDelegate = (AppDelegate) UIApplication.SharedApplication.Delegate;
public YouTubeViewer(string url, RectangleF frame)
{
Log.WriteLog("loading YouTubeView");
appDelegate.firstViewing = true;
this.UserInteractionEnabled = true;
this.BackgroundColor = UIColor.Clear;
this.Frame = frame;
string youTubeVideoHTML = #"<object width=""{1}"" height=""{2}""><param name=""movie""
value=""{0}""></param><embed
src=""{0}"" type=""application/x-shockwave-flash""
width=""{1}"" height=""{2}""</embed></object>";
string html = string.Format(youTubeVideoHTML, url, frame.Size.Width, frame.Size.Height);
this.LoadHtmlString(html, null);
}
}
Here is how the WebView is disposed of:
public void RemoveWebView(UIWebView inView)
{
try
{
Log.WriteLog("RemoveWebView");
NSUrlCache.SharedCache.RemoveAllCachedResponses();
NSUrlCache.SharedCache.DiskCapacity = 0;
NSUrlCache.SharedCache.MemoryCapacity = 0;
inView.LoadHtmlString("",null);
inView.EvaluateJavascript("var body=document.getElementsByTagName('body')[0];body.style.backgroundColor=(body.style.backgroundColor=='')?'white':'';");
inView.EvaluateJavascript("document.open();document.close()");
inView.StopLoading();
inView.Delegate = null;
inView.RemoveFromSuperview();
inView.Dispose();
}
catch(Exception ex)
{
Log.LogError("RemoveWebView",ex);
}
}
Thanks,
Rick
I talked to Xamarin and they suggested removing the override for orientation management in the AppDelegate.
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIApplication application, UIWindow forWindow)
{ /*... code ...*/ }
After I removed this override my application worked as expected when loading YouTube videos.
This resolved the issue for me. You can still control supported orientations via individual ViewController overrides and globally via the Info.plist file.
https://github.com/nishanil/YouTubePlayeriOS
Hope this sample helps you. It worked well for me.

Monotouch MKMapView UIGestureRecognizer problems

I added 3 gesture recognizers to my MapView in IB, a long press, a pan & a pinch. Their delegate is the file's owner. I set them up like so -
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
PanGestureRecognizer.AddTarget(s => { Console.WriteLine("Pan"); } );
LongPressGestureRecognizer.AddTarget(s => { Console.WriteLine("Long press"); } );
PinchGestureRecognizer.AddTarget(s => { Console.WriteLine("Pinch"); } );
}
I also implement this -
public bool ShouldRecognizeSimultaneously (UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
return true;
}
The problem is, only the Long Press gesture recognizer does anything, the others are completely ignored.
Any ideas/suggestions welcome!
Being fairly new to Monotouch, I didn't realise that when I set the delegate of the MapView in IB to my ViewController, that wouldn't actually work. I needed to create a delegate which is a subclass of UIGestureRecognizerDelegate, and set the delegate of the gestureRecognizer to this, and I added the gestureRecognizer programmatically (though that's probably not necessary) -
private class GestureRecognizerDelegate : UIGestureRecognizerDelegate
{
public override bool ShouldRecognizeSimultaneously (UIGestureRecognizer gestureRecognizer, UIGestureRecognizer otherGestureRecognizer)
{
return true;
}
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
UIPinchGestureRecognizer pinchGestureRecognizer = new UIPinchGestureRecognizer(s => { /* do stuff here */ } );
GestureRecognizerDelegate gestureRecognizerDelegate = new GestureRecognizerDelegate();
pinchGestureRecognizer.Delegate = gestureRecognizerDelegate;
MapView.AddGestureRecognizer(pinchGestureRecognizer);
}
Then, by setting the ZoomEnabled property of the MapView to false, I can control how the map zooms (in my case, I had to prevent the map zooming in beyond a certain threshold, my client wasn't happy with the way you could zoom in & it would then bounce back out to my preset value, which I had working using RegionChanged in the MapView delegate). Don't you love clients!

MonoTouch SupportedInterfaceOrientations not found

In MonoTouch 6.0.10 with SDK 6.1 I have the following in a tabbarcontroller and navigationcontroller:
public override bool ShouldAutorotate()
{
return true;
}
public override UIInterfaceOrientationMask SupportedInterfaceOrientations()
{
var orientations = ParentViewController.GetSupportedInterfaceOrientations();
foreach ( var controller in ViewControllers )
orientations = orientations & controller.GetSupportedInterfaceOrientations();
return orientations;
}
In AppDelegate I have:
public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations (UIApplication application, UIWindow forWindow)
{
return UIInterfaceOrientationMask.All;
}
and in FinishedLaunching I have:
window.RootViewController = tabController;
In the tabbarcontroller and navigationcontroller this gets an error of the form 'HelloWorld.TabController.SupportedInterfaceOrientations() is marked as an override but no suitable method found to override (CS0115).'
Any suggestion appreciated!
Bill.
UIViewController defines GetSupportedInterfaceOrientations which you can override in your UITabBarController and UINavigationController subclasses.
The C# compiler error message (and your code) shows that you're missing the Get prefix.

How to use QLPreviewController in a non-modal way? Why does my code not work?

I have QLPreviewController up and running but I'm using PresentModalViewController() to show the QLPreviewController directly. For reasons beyond explanation, I would like to have my own UIViewController which will create its own view and within that view I would like to use the QLPreviewController. Should be easy I thought, but the code below just does nothing. The QLPreviewControllers ViewDidAppear never gets called. (In my example below, PreviewController inherits from QLPreviewController and encapsulates delegate, preview item and source).
Can somebody explain what is wrong with the code below (besides the fact that it is pointless :-))?
Oh, yeah: in my test scenario, I present the controller below modally. It shows up but witout the preview.
public class OuterPreviewController : UIViewController
{
public OuterPreviewController (QLPreviewControllerDataSource oDataSource) : base()
{
this.oDataSource = oDataSource;
}
private PreviewController oPreviewController;
private QLPreviewControllerDataSource oDataSource;
public override void LoadView ()
{
this.View = new UIView();
this.View.Frame = new RectangleF(0, 0, 500, 500);
this.View.BackgroundColor = UIColor.Red;
}
public override void ViewDidAppear (bool animated)
{
// Code execution comes her. No errors, no issues.
base.ViewDidAppear (animated);
this.oPreviewController = new PreviewController();
this.oPreviewController.DataSource = this.oDataSource;
// Preview controller's view is added but it never shows up.
this.View.AddSubview(this.oPreviewController.View);
this.oPreviewController.View.Frame = this.View.Frame;
this.oPreviewController.View.Center = this.View.Center;
}
public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation)
{
return true;
}
}
Found a solution by coincidence today: all ReloadData() on the preview controller and magically it will show its contents.
This allows to add a QLPreviewController to an existing view as a subview and embed a preview. It also gets you rid of the toolbar which contains the open in menu.

Resources