Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Custom UIView with UITextField: Show the keyboard

$
0
0

Hi!

I created a custom UIView component. See the final result:

image

public partial class UsuarioSenhaUIView : UIView
{
    CGPath path;
    CGContext gctx;
    float x;
    float width;
    float height;
    float y;

    UIImageView imageView;

    public enum ImageIcon { User, Padlock }

    UITextField loginUITextField { get; set; }

    public UsuarioSenhaUIView (ImageIcon imageIcon)
    {
        imageView = new UIImageView();
        imageView.Frame = new RectangleF (13, 11, 16, 16);
        if (imageIcon == ImageIcon.User)
            imageView.Image = UIImage.FromBundle ("Images/User");
        else
            imageView.Image = UIImage.FromBundle ("Images/Padlock");
        AddSubview(imageView);

        loginUITextField = new UITextField ();
        loginUITextField.Frame = new RectangleF (55, 5, 224, 30);
        loginUITextField.Font = UIFont.FromName (AppUtils.MUSEO_500_REGULAR, AppUtils.FONTE_TAMANHO_PEQUENO);
        loginUITextField.VerticalAlignment = UIControlContentVerticalAlignment.Center;
        loginUITextField.BackgroundColor = UIColor.White;
        if (imageIcon == ImageIcon.User)
            loginUITextField.Placeholder = "Usuário";
        else
            loginUITextField.Placeholder = "Senha";

        loginUITextField.ShouldReturn = delegate
        {
            loginUITextField.ResignFirstResponder();
            return true;
        };
        loginUITextField.TextColor = UIColor.Black;//UIColor.FromRGB (204, 204, 204);

        this.Add (imageView);
        this.Add (loginUITextField);
    }

    public override void TouchesEnded (NSSet touches, UIEvent evt)
    {
        base.TouchesEnded (touches, evt);
        loginUITextField.BecomeFirstResponder ();
    }

    public override void Draw (RectangleF rect)
    {
        base.Draw (rect);

        #region Borda

        gctx = UIGraphics.GetCurrentContext ();
        gctx.SetLineWidth(2);
        UIColor.White.SetFill ();
        UIColor.FromRGB(204, 204, 204).SetStroke ();

        x = 0f;
        width = 280f;
        height = 38f;
        y = 0f;

        path = new CGPath ();
        path.AddLines(new PointF[]{
            new PointF(x, y),
            new PointF(width, y),
            new PointF(width, height),
            new PointF(x,height),
            new PointF(x,y)});

        path.CloseSubpath();
        gctx.AddPath(path);

        gctx.DrawPath(CGPathDrawingMode.FillStroke);

        #endregion Borda

        #region Ícone

        gctx = UIGraphics.GetCurrentContext ();
        gctx.SetLineWidth(0);
        UIColor.FromRGB(221, 221, 221).SetFill ();

        x = 1f;
        width = 42f;
        height = 37f;
        y = 1f;

        path = new CGPath ();
        path.AddLines(new PointF[]{
            new PointF(x, y),
            new PointF(width, y),
            new PointF(width, height),
            new PointF(x,height),
            new PointF(x,y)});

        path.CloseSubpath();
        gctx.AddPath(path);

        gctx.DrawPath(CGPathDrawingMode.FillStroke);

        #endregion Ícone

        #region Linha

        gctx = UIGraphics.GetCurrentContext ();
        gctx.SetLineWidth(1);
        UIColor.FromRGB(204, 204, 204).SetStroke ();

        x = 42f;
        height = 37f;
        y = 1f;

        path = new CGPath ();
        path.AddLines(new PointF[]{
            new PointF(x, y),
            new PointF(x, height)});

        path.CloseSubpath();
        gctx.AddPath(path);

        gctx.DrawPath(CGPathDrawingMode.FillStroke);

        #endregion Linha
    }


}

It is a personalized UITextField. But I have a problem and a question:

  1. When I touch the component, it does not show the keyboard. What to do to solve this problem?

  2. My code is a good practice or to achieve this visual result (attached image), I must code differently?


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>