获取Xamarin.Forms中Entry的宽度

时间:2018-11-13 06:25:05

标签: c# xamarin xamarin.forms xamarin.ios

如何在Xamarin.Forms中获得Entry视图的宽度?我正在尝试使iOS中的条目看起来像Android中的默认条目(下面有一行)。

自定义渲染器:

void SetBorderWidth(Controls.Entry control)
{
    Control.BorderStyle = UITextBorderStyle.None;
    var myBox = new UIView(new CGRect(0, 40, <INSERT WIDTH HERE>, 1));
    myBox.BackgroundColor = control.BorderColor.ToUIColor();
    Control.AddSubview(myBox);
}

每当我尝试插入control.Width,control.WidthRequest,control.MinimumWidthRequest时,都不会发生任何事情。但是,如果我输入数字,下面的线会突然显示。

另外,当我打印出width和widthrequest时,它们的值为-1。

1 个答案:

答案 0 :(得分:2)

在您的iOS Entry渲染器中,重写Draw方法,并使用提供的CGRect作为Frame当前UITextField的大小,因为该方法可以称为次(屏幕旋转,调整大小等)。UIView最初创建后保存,只需更新其Frame

类似的东西(我没有我的个人密码,但这应该是“关闭”):

UIView myBox;
public override void Draw(CGRect rect)
{
    base.Draw(rect);
    switch (myBox)
    {
        case null:
            myBox = new UIView(new CGRect(0, 40, rect.Width, 1));
            Control.AddSubview(myBox);
            break;
        default:
            myBox.Frame = new CGRect(0, 40, rect.Width, 1);
            break;
    }
}