Alexander Anikin's blog

My personal blog

Archive for the ‘iOS’ Category

How to implement Scroll To Top functionality for UIWebView in Monotouch

leave a comment »

Someone can say – this is by default! But I have a case when it’s not.

Imagine – we have a UIWebView within content and it’s almost full fills screen size without needs to be scrolled. But content inside scrolling using Javascript (something like iScroll extension). So iOS thinking: “I shouldn’t  scroll anything for this view.”.  I this case you’ll never get scroll to top event. It’s not firing!

But I found a workaround for this issue. All you need to do just:

1.  Create a fullscreen ScrollView with content size much greater than screen size  – let iOS thinks: “I should support scrolling for this huge view”.

2. Put you UIWebView at random number of pixel below the top of the screen.

3. Switch off handling of ScrollToTop for UIWebView.ScrollView. This must be done because if two controls handle ScrollToTop – nothing works.

4. SetContentOffset to this random number of pixes – to make you UIWebView looks like it placed full-screen.

5. Create ScrollViewDelegate to handle firings of ScrollToTop Event.

That’s it!

Here is the sample code:

const int scrollOffset = 100;

var contentSize = new SizeF (320, 3000);

var scrollToPoint = new PointF (0, scrollOffset);

ScrollView = new UIScrollView();

UI = new DesignedFileManager(Settings);

// UI placed 100px (scrollOffset) lower than top of the screen

UI.Frame = new RectangleF(0, scrollOffset, Window.Frame.Width, Window.Frame.Height+scrollOffset);

// this lines must be there because iOS can’t choose who will support scrolling to top

// so UI web view will not handle scroll to top

UI.ScrollView.ScrollEnabled = false;

UI.ScrollView.ScrollsToTop = false;

// set scroll view frame like window frame

ScrollView.Frame = Window.Frame;

// let scrollview handle scrolling to top

ScrollView.ScrollEnabled = true;

ScrollView.ScrollsToTop = true;

// set scroll view content size bigger than window frame to support scrolling

ScrollView.ContentSize = contentSize;

ScrollView.ShowsVerticalScrollIndicator = false;

ScrollView.ShowsHorizontalScrollIndicator = false;

// scroll to 100px lower, so wevview fills full screen

ScrollView.SetContentOffset(scrollToPoint, false);

ScrollView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;

ScrollView.Delegate = new ScrollViewDelegate(UI);



And delegate:

class ScrollViewDelegate : UIScrollViewDelegate


DesignedFileManager UI;

public ScrollViewDelegate(DesignedFileManager ui) : base()


UI = ui;


public override bool ShouldScrollToTop(UIScrollView scrollView)


Trace.WriteLine(“Scroll to Top invoked.”);


return false;




Written by Alex Anikin

February 15, 2013 at 6:45 am

Posted in iOS, Mac, Monodevelop, Monotouch

Memory Warning Received and PushViewController

leave a comment »

It was really  great investigation about why iOS application goes to black screen and craches sometimes.

Fisrst of all, I saw this: Received memory warning. Level 1

After that it goes to black screen. This is very interesting, application was working fine if no applications was started in background (I imagined – there are no free memory… but strange…). But, as I can see later, this is view was unloaded only. Several articles wrote – if view has no SuperView it will be unloaded when UIViewController receive memory warning. But this was obviously our fault. After huge  changes in UI we decided to use only one UIWebView. But some code was moved to new version, and  PushViewController invoke too. This is not clear for me now what exactly change this method, but it do something and view unloading after memory warning receive.  So we just remove this line and no black screen (means unloaded view) now.

Written by Alex Anikin

August 4, 2012 at 7:53 pm

Posted in C#, iOS, Mac, Monodevelop, Monotouch

Predictable memory disposing in Monotouch

leave a comment »

I have tasks:

– do huge memory block allocation in iOS (using Monotouch) application (like list with ~20Mb strings);

– clear list and have predictable memory disposing (using Garbage Collector, it should do this in time, or when we need more mem or etc).

This tasks was born from another big task. I should open huge txt files using QLPreviewController. But all I can see – memory was allocating and never disposing (but this story not about it, this leak Xamarin planning to fix it in 5.3.5 or 5.3.6 release – will wait ).

To check this allocations I used XCode -> Instruments -> Activity Monitor (btw very useful tool – highly recommended!)

My tests :

1. Alloc 20Mb -> check it in Activity Monitor (+20Mb)-> Dispose variable -> Wait and do something with app (alloc more, add, delete items) -> Check -> nothing changes – 20Mb still.

2. Alloc 10*20 Mb items-> Check – my app has almost the memory (if i will try to get more ~10-20Mb  – iOS will close my app) -> Dispose 5 items (~100Mb) -> Waiting (thinking about GC) + do something (thinking about GC again) -> Check  – nothing – ~200Mb

3. The same with 2, but start use GC.Collect (GC.MaxGeneration, GCCollectionMode.Forced); after disposing – Waiting (thinking about GC) + do something (thinking about GC again) -> Check  – nothing – ~200Mb

4. …

n. Wow! -> The same with 3, but after allocation I add GC. AddMemoryPressure (size); and after disposing GC. RemoveMemoryPressure (size); -> Activity monitor shows me that I have memory cleared and available for future allocations.

I can be wrong somewhere and please let me know my mistakes.

Written by Alex Anikin

July 10, 2012 at 9:46 pm

Posted in C#, iOS, Monotouch