简体   繁体   中英

Get width of Entry in Xamarin.Forms

How do I get the width of an Entry view in Xamarin.Forms? I'm trying to make an entry in iOS look like the default entry in Android (the one with the line underneath).

Custom Renderer:

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);
}

Whenever I try to insert either control.Width, control.WidthRequest, control.MinimumWidthRequest nothing happens. But if I put a number the line underneath suddenly shows.

Additionally when I print out the width and widthrequest they have a value of -1.

In your iOS Entry renderer, override the Draw method and use the CGRect provided as the current Frame size of the UITextField , since this method can be called multiple times (screen rotations, resizes, etc... Save your UIView after its initial creation and just update its Frame .

Something like (I do not have my personal code with me, but this should be "close"):

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM