pasluc74669 pasluc74669 - 4 months ago 43
iOS Question

xamarin c# UiScrollingView not scrolling with a vertical list of buttons

i'm developing an ios app with xamarin and only c# code, without any designer.

I implemented a UiScrollView with a long list of buttons as follows:

var sizes = UIScreen.MainScreen.Bounds;// 320, 480
....
scrollView = new UIScrollView(new CGRect(20, 200, sizes.Width - 40, sizes.Height - 200));
scrollView.BackgroundColor = UIColor.Magenta;

View.AddSubview(scrollView);


and then i added the list of buttons:

var m1 = new UIButton(UIButtonType.System);
m1.Frame = new RectangleF(45, -50, 230, 20);
m1.SetTitle("Beautiful That Way", UIControlState.Normal);
m1.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Beautiful That Way" );

var m2 = new UIButton(UIButtonType.System);
m2.Frame = new RectangleF(45, -20, 230, 20);
m2.SetTitle("Boys In The Sky", UIControlState.Normal);
m2.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Boys In The Sky");

var m3 = new UIButton(UIButtonType.System);
m3.Frame = new RectangleF(45, 10, 230, 20); m3.SetTitle("Cos'é l'Amor", UIControlState.Normal);
m3.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Cos'é l'Amor");

var m4 = new UIButton(UIButtonType.System);
m4.Frame = new RectangleF(45, 40, 230, 20);
m4.SetTitle("Dancing Qeen", UIControlState.Normal);
m4.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Dancing Qeen");

var m5 = new UIButton(UIButtonType.System);
m5.Frame = new RectangleF(45, 70, 230, 20);
m5.SetTitle("Free", UIControlState.Normal);
m5.TouchUpInside += (sender, e) => OpenPlayerView(PlayerOption.Stream, "mySongUrl.mp3", "Free");

scrollView.AddSubview(m1);
scrollView.AddSubview(m2);
scrollView.AddSubview(m3);
scrollView.AddSubview(m4);
scrollView.AddSubview(m5);


I need to add up to 60 buttons, and the UiScrollView does not scrolls...

Any idea how to solve?
Thanks in advance...

Answer

You need to tell the scroll view the size of the content within it, something like:

scrollView.ContentSize = new CGSize(UIScreen.MainScreen.Bounds.Width, 50 * 40);

Example:

scrollView = new UIScrollView(new CGRect(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height));
for (var i = 0; i < 50; i++)
{
    var button = new UIButton(UIButtonType.System);
    button.Frame = new CGRect(40, 20 + (i * 40), 100, 40);
    button.SetTitle("Button : " + i, UIControlState.Normal);
    scrollView.Add(button);
}
scrollView.ContentSize = new CGSize(UIScreen.MainScreen.Bounds.Width, 50 * 40);
Add(scrollView);