Ich habe gelernt, dass wir die UISwitch-Schaltfläche in ihrem "Ein" -Zustand ändern können, , aber es ist auch möglich, die Farbe des UISwitch im "Aus" -Zustand zu ändern ?Ändern der Farbe des UISwitch im "Aus" -Zustand
Antwort
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
Awesome ... Arbeitete für mich ... Ich lief rum und machte alle möglichen Anpassungen, um dies zu erreichen: p – JgdGuy
Einstellung TintColor in iOS7 entfernt "Umriss" für mich (Farbton weiß vor weißem Hintergrund). –
Versuchen Sie sich mit diesem
yourSwitch.backgroundColor = [UIColor whiteColor];
youSwitch.layer.cornerRadius = 16.0;
All dank @Barry Wyckoff.
Das ist die richtige Antwort :) setTint ändert auch die "Outline" -Farbe, die den Hintergrund auf weißem Hintergrund visuell "verbirgt". –
Beachten Sie, dass der Hintergrund rechteckig ist. –
das ist richtig @ Lukasz'Severiaan'Grela – Sourabh
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:
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
Farbe ändern von Attributen Inspektor
Ausgabe
Es gibt keine richtigen out setzen in swift 3 –
@KetanP können Sie Problem genauer erklären? –
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
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
}
}
}
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)
}
}
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
}
haben Sie tintColor Eigenschaft verwendet, um Farbe zu ändern? – rishi