I have a forms application and created a ViewRenderer for iOS. I need to get the frame of that view, but the Frame X and Y is wrong (depending a bit on how I lay them out in the Xaml how much wrong).
It seams like I it gets it X an Ys before all layout has re-sized it self ?
Getting extremely frustrated with this! Anyone any tips or ideas ?
Her is some code - CustomRenderer in iOS
using System;
using System.Collections.Generic;
using System.Windows.Input;
using CoreGraphics;
using Test.CustomViews;
using Test.iOS.CustomRendere;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(PopupListSelector), typeof(CustomPopupListSelectorRenderer))]
namespace Test.iOS.CustomRendere
{
public class CustomPopupListSelectorRenderer: ViewRenderer
{
UIViewController viewController;
public override void TouchesBegan(Foundation.NSSet touches, UIEvent evt)
{
base.TouchesBegan(touches, evt);
ShowPopup();
}
public void ShowPopup()
{
var topController = SetTopViewController();
viewController = new UIViewController();
UIPopoverPresentationController pc = (UIPopoverPresentationController)viewController.PresentationController;
//this.SetNeedsLayout();
//this.LayoutIfNeeded();
pc.SourceView = topController.View;
pc.SourceRect = this.Frame;
pc.PermittedArrowDirections = UIPopoverArrowDirection.Left;
UITableView table = new UITableView(new CGRect(0, 0, 300, 300));
table.CellLayoutMarginsFollowReadableWidth = false;
viewController.Add(table);
topController.PresentViewController(viewController, true, null);
}
private UIViewController SetTopViewController()
{
var topController = UIApplication.SharedApplication.KeyWindow.RootViewController;
while (topController.PresentedViewController != null)
{
topController = topController.PresentedViewController;
}
return topController;
}
}
}
Her is the ContentView in the PCL
using System;
using System.Collections.Generic;
using System.Windows.Input;
using Xamarin.Forms;
namespace Test.CustomViews
{
public class PopupListSelector : ContentView
{
private readonly Label textLabel;
private readonly Label itemSelectedText;
private readonly StackLayout stacklayout;
public PopupListSelector()
{
stacklayout = new StackLayout()
{
Orientation = StackOrientation.Horizontal,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.Center,
Padding = new Thickness(5),
};
textLabel = new Label { Text = Text, HorizontalOptions = LayoutOptions.FillAndExpand };
itemSelectedText = new Label { Text = Text, HorizontalOptions = LayoutOptions.End};
stacklayout.Children.Add(textLabel);
stacklayout.Children.Add(itemSelectedText);
Content = stacklayout;
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
textLabel.Text = value;
}
}
public static BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(PopupListSelector), string.Empty,
BindingMode.TwoWay, null, (bindable, oldValue, newValue) =>
{
var ctrl = (PopupListSelector)bindable;
ctrl.Text = (string)newValue;
});
}
}
and the Xaml
<?xml version="1.0" encoding="utf-8" ?>
<popup:PopupPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:popup="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
xmlns:views="clr-namespace:CornerStone.CustomViews;assembly=CornerStone"
x:Class="Test.Views.Page">
<popup:PopupPage.Animation>
<animations:ScaleAnimation
PositionIn="Center"
PositionOut="Center"
ScaleIn="1.2"
ScaleOut="0.8"
DurationIn="400"
DurationOut="300"
EasingIn="SinOut"
EasingOut="SinIn"
HasBackgroundAnimation="True"/>
</popup:PopupPage.Animation>
<Grid BackgroundColor="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24*"/>
<ColumnDefinition Width="1"/>
<ColumnDefinition Width="75*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ScrollView Grid.Row="0" Grid.Column="0" Orientation="Vertical" HorizontalOptions="FillAndExpand">
<views:PopupListSelector
Text = "TEST">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST2">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST3">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST4">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST5">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST6">
</views:PopupListSelector>
</ScrollView>
<BoxView Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" WidthRequest="1" VerticalOptions="FillAndExpand"/>
<StackLayout Grid.Row="1" Grid.Column="2" Orientation="Vertical">
<views:PopupListSelector
Text = "TEST7">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST8">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST9">
</views:PopupListSelector>
<views:PopupListSelector
Text = "TEST10">
</views:PopupListSelector>
</StackLayout>
</Grid>
</popup:PopupPage>