2015-11-26 18 views
8

Ho costruito una vista che appare dal basso e si nasconde dopo un po 'e funziona bene, ma voglio farlo nella modalita' UIView come modale, ho guardato su internet ma non potevo ottenere o capire come farlo.Classe UIView personalizzata - Swift

snake = UIView(frame: CGRect(x: 0 , y: self.view.frame.size.height-66, width: self.view.frame.size.width, height: 66)) 
snake.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0) 


let label = UILabel(frame: CGRect(x: 12, y: 8, width: snake.frame.size.width-90, height: 50)) 
label.text = "Connection error please try again later!!" 
label.textColor = UIColor.whiteColor() 
label.numberOfLines = 0 
label.font = UIFont.systemFontOfSize(14) 
snake.addSubview(label) 

let button = UIButton(frame: CGRect(x: snake.frame.size.width-87, y: 8, width: 86, height: 50)) 
button.setTitle("OK", forState: UIControlState.Normal) 
button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal) 
button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside) 
snake.addSubview(button) 
self.view.addSubview(snake) 

come iniziare con questa classe ho idea, io sto creando la vista a livello di codice con la sua cornice e ho bisogno di impostare le proprietà per il pulsante per esempio, o l'etichetta e creare la vista da ogni classe.

class snake: UIView { 


    override init (frame : CGRect) { 
     super.init(frame : frame) 

    } 

    convenience init() { 
     self.init(frame:CGRect.zero) 
    } 

    required init(coder aDecoder: NSCoder) { 
     fatalError("This class does not support NSCoding") 
    } 

} 

risposta

13

codice:

var label:UILabel! 
var button:UIButton! 

override init (frame : CGRect) { 
    super.init(frame : frame) 
    self.backgroundColor = UIColor(red: 50/255, green: 50/255, blue: 50/255, alpha: 1.0) 


    label = UILabel(frame: CGRect(x: 12, y: 8, width: self.frame.size.width-90, height: 50)) 
    label.text = "Connection error please try again later!!" 
    label.textColor = UIColor.whiteColor() 
    label.numberOfLines = 0 
    label.font = UIFont.systemFontOfSize(14) 
    self.addSubview(label) 

    button = UIButton(frame: CGRect(x: self.frame.size.width-87, y: 8, width: 86, height: 50)) 
    button.setTitle("OK", forState: UIControlState.Normal) 
    button.setTitleColor(UIColor(red: 76/255, green: 175/255, blue: 80/255, alpha: 1.0), forState: UIControlState.Normal) 
    button.addTarget(self, action: "hideSnackBar:", forControlEvents: UIControlEvents.TouchUpInside) 
    self.addSubview(button) 
} 

e usarlo:

let snakeView = snake(frame: CGRectMake(0 ,self.view.frame.size.height-66, self.view.frame.size.width, 66))) 

e impostare i dati per snakeview:

snakeView.label.text = "hello" 

Ma di solito i 'll creare una funzione per aggiornare dati per la vista:

func updateData(title:String){ 
    self.label.text = title 
} 

e chiamare quando necessità:

snake.updateData("hello") 

P/s: se si usa XI ter, è necessario implementare awakeFromNib invece di init. E creare serpente con XI ter (ricordate set identifier per XI ter: "snakeView"):

let snakeView = NSBundle.mainBundle().loadNibNamed("snakeView", owner: nil, options: nil)[0] as snakeView 
+0

Bene grande, ma ancora una domanda se ho bisogno di impostare una proprietà per l'etichetta quando ho bisogno di utilizzare la vista, come è è possibile? ho bisogno di un setter ma puoi mostrarmi un esempio? –

+0

Ho aggiornato il mio post –

+0

@Nguyen Hoan invece di una classe, come spingere questo codice in una struttura? –

Problemi correlati