2012-04-27 6 views

Antwort

27

Sie können die Eigenschaft tintColor auf dem Switch verwenden.

switch.tintColor = [UIColor redColor]; // the "off" color 
switch.onTintColor = [UIColor greenColor]; // the "on" color 

Hinweis: dieses iOS 5+ erfordert

+0

Awesome ... Arbeitete für mich ... Ich lief rum und machte alle möglichen Anpassungen, um dies zu erreichen: p – JgdGuy

+3

Einstellung TintColor in iOS7 entfernt "Umriss" für mich (Farbton weiß vor weißem Hintergrund). –

93

Versuchen Sie sich mit diesem

yourSwitch.backgroundColor = [UIColor whiteColor]; 
youSwitch.layer.cornerRadius = 16.0; 

All dank @Barry Wyckoff.

+2

Das ist die richtige Antwort :) setTint ändert auch die "Outline" -Farbe, die den Hintergrund auf weißem Hintergrund visuell "verbirgt". –

+0

Beachten Sie, dass der Hintergrund rechteckig ist. –

+0

das ist richtig @ Lukasz'Severiaan'Grela – Sourabh

70

Meine Lösung mit # swift2:

let onColor = _your_on_state_color 
let offColor = _your_off_state_color 

let mSwitch = UISwitch(frame: CGRectZero) 
mSwitch.on = true 

/*For on state*/ 
mSwitch.onTintColor = onColor 

/*For off state*/ 
mSwitch.tintColor = offColor 
mSwitch.layer.cornerRadius = 16 
mSwitch.backgroundColor = offColor 

Ergebnis:

enter image description here

+0

sehr schön danke – ashokdy

+0

arbeitet Geldbußen für xcode 7.1 –

+0

Gute Wahl! Arbeitete für mich. Vielen Dank. – Felipe

18

Swift IBDesignable

import UIKit 
@IBDesignable 

class UISwitchCustom: UISwitch { 
    @IBInspectable var OffTint: UIColor? { 
     didSet { 
      self.tintColor = OffTint 
      self.layer.cornerRadius = 16 
      self.backgroundColor = OffTint 
     } 
    } 
} 

Setklasse in Identity Inspektor

enter image description here

Farbe ändern von Attributen Inspektor

enter image description here

Ausgabe

enter image description here

+0

Es gibt keine richtigen out setzen in swift 3 –

+0

@KetanP können Sie Problem genauer erklären? –

0

Ziel c Kategorie auf jedem UISlider in Projekt verwenden mithilfe von Code oder Storyboard:

#import <UIKit/UIKit.h> 

@interface UISwitch (SAHelper) 
@property (nonatomic) IBInspectable UIColor *offTint; 
@end 

Implementierung

#import "UISwitch+SAHelper.h" 

@implementation UISwitch (SAHelper) 
@dynamic offTint; 
- (void)setOffTint:(UIColor *)offTint { 
    self.tintColor = offTint; //comment this line to hide border in off state 
    self.layer.cornerRadius = 16; 
    self.backgroundColor = offTint; 
} 
@end 
3

Der beste Weg, die Hintergrundfarbe & Größe UISwitch

Vorerst zu verwalten es ist Swift 2.3 Code

import Foundation 
import UIKit 

@IBDesignable 
class UICustomSwitch : UISwitch { 

    @IBInspectable var OnColor : UIColor! = UIColor.blueColor() 
    @IBInspectable var OffColor : UIColor! = UIColor.grayColor() 
    @IBInspectable var Scale : CGFloat! = 1.0 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
     self.setUpCustomUserInterface() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.setUpCustomUserInterface() 
    } 


    func setUpCustomUserInterface() { 

     //clip the background color 
     self.layer.cornerRadius = 16 
     self.layer.masksToBounds = true 

     //Scale down to make it smaller in look 
     self.transform = CGAffineTransformMakeScale(self.Scale, self.Scale); 

     //add target to get user interation to update user-interface accordingly 
     self.addTarget(self, action: #selector(UICustomSwitch.updateUI), forControlEvents: UIControlEvents.ValueChanged) 

     //set onTintColor : is necessary to make it colored 
     self.onTintColor = self.OnColor 

     //setup to initial state 
     self.updateUI() 
    } 

    //to track programatic update 
    override func setOn(on: Bool, animated: Bool) { 
     super.setOn(on, animated: true) 
     updateUI() 
    } 

    //Update user-interface according to on/off state 
    func updateUI() { 
     if self.on == true { 
      self.backgroundColor = self.OnColor 
     } 
     else { 
      self.backgroundColor = self.OffColor 
     } 
    } 
} 
0

sicherer Weg in Swift 3 ohne magische 16pt Werte:

class ColoredBackgroundSwitch: UISwitch { 

    var offTintColor: UIColor { 
    get { 
     return backgroundColor ?? UIColor.clear 
    } 
    set { 
     backgroundColor = newValue 
    } 
    } 

    override func layoutSubviews() { 
    super.layoutSubviews() 
    let minSide = min(frame.size.height, frame.size.width) 
    layer.cornerRadius = ceil(minSide/2) 
    } 

} 
0

Swift 4 einfachste und schnellste Weg, um es in 3 Schritten zu erhalten:

// background color is the color of the background of the switch 
switchControl.backgroundColor = UIColor.white.withAlphaComponent(0.9) 

// tint color is the color of the border when the switch is off, use 
// clear if you want it the same as the background, or different otherwise 
switchControl.tintColor = UIColor.clear 

// and make sure that the background color will stay in border of the switch 
switchControl.layer.cornerRadius = integrationSwitch.bounds.height/2 

Wenn Sie manuell die Größe des Schalters ändern (zB durch automatischen Layout verwenden), müssen Sie Aktualisieren Sie auch die switch.layer.cornerRadius, zDurch layoutSubviews zwingende und nach dem Aufruf von Super der Eckradius Aktualisierung:

override func layoutSubviews() { 
    super.layoutSubviews() 
    switchControl.layer.cornerRadius = integrationSwitch.bounds.height/2 
}