2016-01-04 9 views
7

Chcę utworzyć poziomego gradientu od lewej do prawej na każdej komórce używam CAGradientLayey aby utworzyć warstwę niestandardową i dodać tę warstwę do komórki tableviewustawić nachylenie poziome w UITableViewCell Swift

To jest mój kod w szybkiej

func gradient(frame:CGRect) -> CAGradientLayer { 
    let layer = CAGradientLayer() 
    layer.frame = frame 
    print(frame) 
    layer.startPoint = CGPointMake(0,0.5) 
    layer.endPoint = CGPointMake(1,0.5) 
    layer.colors = [ UIColor.redColor().CGColor,UIColor.blueColor().CGColor] 
    return layer 
} 

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, 
    forRowAtIndexPath indexPath: NSIndexPath) { 
     cell.layer.addSublayer(gradient(cell.frame)) 
      cell.textLabel?.textColor = UIColor(red:0, green:0.102, blue: 0.2, alpha: 1) 

} 

Ale gradient nie obejmuje całej komórki widoku tabeli, przełącza się między komórką gradientu a normalnym kolorem tła komórki i wydaje się, że gradient całkowicie pokrywa tekst komórki. Nie wiem, co robię źle

Wszelkie sugestie? Dziękuję

Odpowiedz

5

Zamiast frame użyć bounds komórki również insertSublayer o indeksie 0, więc to przyjdzie za wszystkie inne warstwy w tym etykiety

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, 
    forRowAtIndexPath indexPath: NSIndexPath) { 
     cell.layer.insertSublayer(gradient(cell.bounds), atIndex:0) 
     cell.backgroundColor = UIColor.clearColor() 
     cell.textLabel?.textColor = UIColor(red:0, green:0.102, blue: 0.2, alpha: 1)    
} 
+0

Że to zrobił ale tekst komórka jest nadal brakuje . Jakieś sugestie? dziękuję – sin90

+0

To wykonało zadanie Dziękuję! – sin90