Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

ListView - Enabling Different Cell Heights in iOS

$
0
0

As highlighted by Craig Dunn here:
developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/listview/

"On iOS the developer must then calculate (or estimate) the height of each cell - Xamarin.Forms cannot currently pre-render the cell in order to determine its natural height based on its contents. "

I been using the suggested work around code from the blog; however, whilst it works Ok in portrait mode on iPhones, it of course doesn't take into consideration landscape mode or iPad tablets.

How do I check in Xamarin.Forms if the iOS device is in portrait or landscape and if the device is an iPad?

const int avgCharsInRow = 35;
const int defaultHeight = 44;
const int extraLineHeight = 20;
protected override void OnBindingContextChanged ()
{
base.OnBindingContextChanged ();
if (Device.OS == TargetPlatform.iOS) { // don't bother on the other platforms
var text = (string)BindingContext;
var len = text.Length;
if (len < (avgCharsInRow * 2)) {
// fits in one cell
Height = defaultHeight;
} else {
len = len - (avgCharsInRow * 2);
var extraRows = len / 35;
Height = defaultHeight + extraRows * extraLineHeight;
}
}
}


Viewing all articles
Browse latest Browse all 58056

Trending Articles