use*_*878 81 iphone uiswitch ios
我已经了解到我们可以将UISwitch按钮外观更改为"on"状态,但是也可以在"关闭"状态下更改UISwitch的颜色吗?
Sou*_*waj 125
试试这个
yourSwitch.backgroundColor = [UIColor whiteColor];
youSwitch.layer.cornerRadius = 16.0;
Run Code Online (Sandbox Code Playgroud)
非常感谢@Barry Wyckoff.
Lon*_*ham 121
我用#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 = mSwitch.frame.height / 2
mSwitch.backgroundColor = offColor
Run Code Online (Sandbox Code Playgroud)
结果:
ben*_*oom 35
您可以tintColor在交换机上使用该属性.
switch.tintColor = [UIColor redColor]; // the "off" color
switch.onTintColor = [UIColor greenColor]; // the "on" color
Run Code Online (Sandbox Code Playgroud)
请注意,这需要iOS 5+
Afz*_*mad 27
Swift IBDesignable
import UIKit
@IBDesignable
class UISwitchCustom: UISwitch {
@IBInspectable var OffTint: UIColor? {
didSet {
self.tintColor = OffTint
self.layer.cornerRadius = 16
self.backgroundColor = OffTint
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Identity检查器中设置类
从"属性"检查器更改颜色
产量
斯威夫特 5:
import UIKit
extension UISwitch {
func set(offTint color: UIColor ) {
let minSide = min(bounds.size.height, bounds.size.width)
layer.cornerRadius = minSide / 2
backgroundColor = color
tintColor = color
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的应用程序需要其他开关,那么在自定义类中实现@LongPham 的代码也可能是一个好主意。正如其他人指出的那样,对于“关闭”状态,您还需要更改背景颜色,因为默认值是透明的。
class MySwitch: UISwitch {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
// Setting "on" state colour
self.onTintColor = UIColor.green
// Setting "off" state colour
self.tintColor = UIColor.red
self.layer.cornerRadius = self.frame.height / 2
self.backgroundColor = UIColor.red
}
}
Run Code Online (Sandbox Code Playgroud)


工作 100% IOS 13.0 和 Swift 5.0 切换两个状态颜色设置相同#ios13 #swift #swift5
@IBOutlet weak var switchProfile: UISwitch!{
didSet{
switchProfile.onTintColor = .red
switchProfile.tintColor = .red
switchProfile.subviews[0].subviews[0].backgroundColor = .red
}
}
Run Code Online (Sandbox Code Playgroud)
管理UISwitch背景颜色和大小的最佳方法
现在它是Swift 2.3代码
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
在Swift 4+中:
off 州:
switch.tintColor = UIColor.blue
Run Code Online (Sandbox Code Playgroud)
on 州:
switch.onTintColor = UIColor.red
Run Code Online (Sandbox Code Playgroud)
这是一个很好的技巧:您可以直接进入UISwitch的子视图,该子视图绘制其“关闭”背景,并更改其背景颜色。在iOS 13中,此方法比在iOS 12中更好:
if #available(iOS 13.0, *) {
self.sw.subviews[0].subviews[0].backgroundColor = .green
} else if #available(iOS 12.0, *) {
self.sw.subviews[0].subviews[0].subviews[0].backgroundColor = .green
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
45429 次 |
| 最近记录: |