2015-07-27 13 views

risposta

25

È possibile rimuovere sia i bordi che i divisori utilizzando la funzione seguente. Creare estensione UISegmentedControl:

For Swift 2.2:

extension UISegmentedControl { 
    func removeBorders() { 
     setBackgroundImage(imageWithColor(backgroundColor!), forState: .Normal, barMetrics: .Default) 
     setBackgroundImage(imageWithColor(tintColor!), forState: .Selected, barMetrics: .Default) 
     setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default) 
    } 

    // create a 1x1 image with this color 
    private func imageWithColor(color: UIColor) -> UIImage { 
     let rect = CGRectMake(0.0, 0.0, 1.0, 1.0) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext() 
     CGContextSetFillColorWithColor(context, color.CGColor); 
     CGContextFillRect(context, rect); 
     let image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return image 
    } 
} 

For Swift 3:

extension UISegmentedControl { 
    func removeBorders() { 
     setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default) 
     setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default) 
     setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) 
    } 

    // create a 1x1 image with this color 
    private func imageWithColor(color: UIColor) -> UIImage { 
     let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) 
     UIGraphicsBeginImageContext(rect.size) 
     let context = UIGraphicsGetCurrentContext() 
     context!.setFillColor(color.cgColor); 
     context!.fill(rect); 
     let image = UIGraphicsGetImageFromCurrentImageContext(); 
     UIGraphicsEndImageContext(); 
     return image! 
    } 
} 

Chiamare la funzione di cui sopra.

segmentedControl.removeBorders() 

Riferimento: Remove UISegmentedControl separators completely. (iphone)

Grazie a https://stackoverflow.com/users/3921490/amagain per Swift 3 versione.

+0

Thanks :) Ha funzionato = D Ma ancora una domanda, questo renderà il mio controllo segmentato hanno un angolo di forma rettangolare acuto, eventuali modi per rendere arrotondato? – Happiehappie

+0

@Happiehappie no non puoi, per favore fai riferimento a questa risposta http://stackoverflow.com/questions/1834244/uisegmentedcontrol-without-rounded-corner –

+0

Sto ottenendo l'errore fatale: inaspettatamente trovato nil mentre scartando un valore Optional' on line 'setBackgroundImage (imageWithColor (color: backgroundColor!), per: .normal, barMetrics: .default)' – Kakaji

9

Ecco la versione 3 rapida della risposta di Sohil che potrebbe aiutare qualcun altro. Mi ha aiutato. :)

extension UISegmentedControl { 
func removeBorders() { 
    setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default) 
    setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default) 
    setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) 
} 

// create a 1x1 image with this color 
private func imageWithColor(color: UIColor) -> UIImage { 
    let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0) 
    UIGraphicsBeginImageContext(rect.size) 
    let context = UIGraphicsGetCurrentContext() 
    context!.setFillColor(color.cgColor); 
    context!.fill(rect); 
    let image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return image! 
    } 
} 
1

Se si desidera salvare i confini tra le cellule

extension UISegmentedControl { 
    func removeBorders() { 
    if let backgroundColor = backgroundColor, let backgroundImage = UIImage.imageWithSize(size: CGSize.one_one, color: backgroundColor){ 
     setBackgroundImage(backgroundImage, for: .normal, barMetrics: .default) 
    } 

    if let tintColor = tintColor, let tintImage = UIImage.imageWithSize(size: CGSize.one_one, color: tintColor){ 
     setBackgroundImage(tintImage, for: .selected, barMetrics: .default) 
     setDividerImage(tintImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default) 
    } 
    } 
} 

extension CGSize{ 
    static var one_one: CGSize{ 
    return CGSize(width: 1.0, height: 1.0) 
    } 
} 

extension UIImage{ 
    static func imageWithSize(size : CGSize, color : UIColor = UIColor.white) -> UIImage? { 
    var image:UIImage? = nil 
    UIGraphicsBeginImageContext(size) 
    if let context = UIGraphicsGetCurrentContext() { 
     context.setFillColor(color.cgColor) 
     context.addRect(CGRect(origin: CGPoint.zero, size: size)); 
     context.drawPath(using: .fill) 
     image = UIGraphicsGetImageFromCurrentImageContext(); 
    } 
    UIGraphicsEndImageContext() 
    return image 
    } 
} 
Problemi correlati