El borde de UIButton extendido no se dibuja inicialmente


Estoy tratando de crear un UIButton personalizado que se extiende desde UIButtonType.RoundedRect.

Mi funcionalidad agregada está funcionando, pero hay un problema con el estado de borde redondeado inicial de mi botón. El borde de mi botón extendido no se dibuja hasta después de que se ha tocado.

Captura de pantalla Antes-Después

Actualización (24 de enero de 2013): Se agregó el resultado de la prueba de fondo rojo, según lo solicitado por Richard Marskell, que concluye que solo se dibuja la etiqueta del botón. BackgroundColor = UIColor.Red;

Prueba de Fondo Rojo, Captura de Pantalla Antes-Después

A continuación se muestra mi código fuente para crear mi botón personalizado.

public class TestView : UIView
{
    public TestView(IntPtr p) : base(p) { }

    public TestView(RectangleF bounds)
    {
        Frame = bounds;
        BackgroundColor = UIColor.White;

        UIButton button1 = new UIButton(UIButtonType.RoundedRect);
        button1.Frame = new RectangleF(20,20,100,50);
        button1.SetTitle("Button 1", UIControlState.Normal);
        AddSubview(button1); // Drawn Correctly

        MyButton button2 = new MyButton();
        button2.Frame = new RectangleF(20,90,100,50);
        button2.SetTitle("Button 2", UIControlState.Normal);
        AddSubview(button2); // Only drawn correctly after Tap

        // EDIT: Added to test Miguel's theory
        UIButton button3 = UIButton.FromType(UIButtonType.RoundedRect);
        button3.Frame = new RectangleF(20,160,100,50);
        button3.SetTitle("Button 3", UIControlState.Normal);
        AddSubview(button3); // Drawn Correctly
    }
}

public class MyButton : UIButton
{
    public MyButton() : base(UIButtonType.RoundedRect) { }
}
  • Simplemente no estoy seguro de cómo forzar que el borde se dibuje correctamente al cargar la vista.
  • No necesito un botón de tipo UIButtonType.Custom, ya que no quiero estilizar el botón yo mismo.
  • Cuando depuro I el tipo de myButton se establece correctamente en UIButtonType.RoundedRect.
  • Las propiedades base de UIButton de myButton (button2) coinciden con las propiedades de la instancia de UIButton (button1).

Depurar

¿Cómo puedo resolver este problema?


Actualización (31 de enero de 2013): Herman Schoenfeld proporcionó una solución adecuada para este error.

Author: Scott, 2012-12-29

1 answers

Esto funciona

public class MyButton : UIButton
{
    public MyButton() : base(UIButtonType.RoundedRect) { }


    public override RectangleF Frame {
        get {
            return base.Frame;
        }
        set {
            var temp = TranslatesAutoresizingMaskIntoConstraints;
            TranslatesAutoresizingMaskIntoConstraints = false;
            var constraints = new [] {
                NSLayoutConstraint.Create(this, NSLayoutAttribute.Width, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, value.Width),
                NSLayoutConstraint.Create(this, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1.0f, value.Height)
            };
            AddConstraints(constraints);
            SizeToFit();
            RemoveConstraints(constraints);
            base.Frame = value;
            TranslatesAutoresizingMaskIntoConstraints = temp;
        }
    }
}

Esto es solo una solución, parece ser un error. sizeToFit () soluciona el problema, el otro código mantiene el marco.

 7
Author: Herman Schoenfeld,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-01 01:47:14