2012-12-29 15 views
31

Sto provando a creare un UIButton personalizzato che si estende da UIButtonType.RoundedRect.Il bordo UIButton esteso non viene inizialmente disegnato

La mia funzionalità aggiunta funziona, ma c'è un problema con lo stato del bordo arrotondato iniziale del mio pulsante. Il bordo del mio pulsante esteso non viene disegnato fino a quando non viene toccato.

Before-After Screenshot

Aggiornamento (24 gennaio 2013): Inserito il risultato del fondo di prova rosso, come richiesto da Richard Marskell, che conclude solo l'etichetta del pulsante viene disegnato. BackgroundColor = UIColor.Red;

Red Background Test, Before-After Screenshot

Qui di seguito è il mio codice sorgente per creare il mio pulsante personalizzato.

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) { } 
} 
  • io non sono solo sicuro di come forzare il confine per essere disegnato correttamente sul carico della vista.
  • Non ho bisogno di un pulsante di tipo UIButtonType.Custom, poiché non desidero applicare lo stile al pulsante.
  • Quando eseguo il debug I il tipo di MyButton è impostato correttamente su UIButtonType.RoundedRect.
  • Le proprietà di base UIButton di MyButton (button2) corrispondono alle proprietà dell'istanza UIButton (pulsante1).

Debug

Come posso risolvere questo problema?


Aggiornamento (31 gennaio 2013): Herman Schoenfeld ha fornito una soluzione adeguata per questo errore.

+6

+1 per una domanda ben formattata. – MarioDS

+0

Puoi provare il metodo UIButton.FromXXX per creare il pulsante al posto del costruttore? Solo per provare una teoria –

+0

@ miguel.de.icaza Grazie per aver esaminato questo. Ho provato a creare un UIButton usando il metodo 'UIButton.FromType', e viene disegnato correttamente. Non sono sicuro di come dovrei usare questo metodo per creare un'istanza di MyButton usando questo metodo. – Scott

risposta

7

Questo funziona

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

questa è solo una soluzione, sembra essere un bug. SizeToFit() risolve il problema, l'altro codice mantiene il frame.

+0

Grazie Herman, una buona soluzione a questo bug. Ho cancellato la proprietà 'Frame' del mio pulsante in modo che la correzione sia applicata senza la necessità di chiamare il metodo separato' Fix() '. – Scott

+0

Aggiornato con il tuo suggerimento. –

Problemi correlati