Forked from andywhitt/MailViewTestViewController.cs
Created
June 24, 2013 08:14
Revisions
-
andywhitt created this gist
May 9, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,97 @@ using System; using System.Drawing; using System.IO; using System.Net; using MonoTouch.Foundation; using MonoTouch.UIKit; // A MonoTouch iOS Mail like example with a custom view and a UIWebView : // // http://stackoverflow.com/questions/10434444/zoom-a-uiwebview-like-ios-mail-app/10463431 // // andywhitt[at]gmail.com namespace MailViewTest { public partial class MailViewTestViewController : UIViewController { UILabel customView; // customview, in this example its just a UILabel UIView fakeBackgroundView = new UIView(); private bool _loaded; public MailViewTestViewController () : base ("MailViewTestViewController", null) { } public override void ViewDidLoad () { base.ViewDidLoad (); customView = new UILabel (new RectangleF (0, 0, View.Frame.Width, 50)) { Text = "Testing Text" }; webView.BackgroundColor = UIColor.ScrollViewTexturedBackgroundColor; webView.LoadFinished += HandleWebViewLoadFinished; webView.ScalesPageToFit = true; // grab some html var request = WebRequest.Create ("http://www.stackoverflow.com"); var response = request.GetResponse (); var reader = new StreamReader (response.GetResponseStream ()); // HACK : add ViewPort meta tag so that if the page is smaller than the View it will zoom correctly var str = "<meta name=\"viewport\" content=\"width=320\"/>" + reader.ReadToEnd (); webView.LoadHtmlString (str, null); webView.ScrollView.WeakDelegate = this; webView.ScrollView.AddSubview (customView); // remove the webviews shadow and update all others subviews Y foreach (var subview in webView.ScrollView.Subviews) { if (subview == customView) continue; else if (subview is UIImageView) // remove shadow subview.Hidden = true; else subview.Frame = new RectangleF (new PointF (subview.Frame.X, subview.Frame.Y + customView.Frame.Height), subview.Frame.Size); } } [Export("scrollViewDidScroll:")] private void HandleWebViewScrollViewScrolled (UIScrollView scrollView) { float x = 0; if(scrollView.ContentOffset.X < 0) x = 0; else if((scrollView.ContentOffset.X + customView.Frame.Size.Width) > scrollView.ContentSize.Width) x = scrollView.ContentSize.Width - customView.Frame.Size.Width; else x = scrollView.ContentOffset.X; customView.Frame = new RectangleF(new PointF(x, customView.Frame.Y), customView.Frame.Size); } private void HandleWebViewLoadFinished (object sender, EventArgs e) { if (_loaded) return; // add the fake white background so it looks like Mail fakeBackgroundView.BackgroundColor = UIColor.White; fakeBackgroundView.Frame = new RectangleF (new PointF (0, 0), webView.ScrollView.ContentSize); webView.ScrollView.AddSubview (fakeBackgroundView); webView.ScrollView.SendSubviewToBack (fakeBackgroundView); _loaded = true; } public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation) { // update the fakebackground frame fakeBackgroundView.Frame = new RectangleF (new PointF (0, 0), webView.ScrollView.ContentSize); } public override bool ShouldAutorotateToInterfaceOrientation (UIInterfaceOrientation toInterfaceOrientation) { // Return true for supported orientations return (toInterfaceOrientation != UIInterfaceOrientation.PortraitUpsideDown); } } }