2012-04-27 44 views

Trả lời

27

Bạn có thể sử dụng thuộc tính tintColor trên nút gạt.

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

Lưu ý điều này đòi hỏi iOS 5 +

+0

Tuyệt vời ... Làm việc cho tôi ... Tôi chạy xung quanh làm tất cả các loại điều chỉnh để đạt được điều này: p – JgdGuy

+3

Cài đặt tintColor trong iOS7 loại bỏ "đường viền" cho tôi (màu trắng đối với nền trắng). –

93

Hãy thử sử dụng

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

này Tất cả nhờ vào @Barry Wyckoff.

+2

NÀY là câu trả lời đúng :) setTint thay đổi màu "phác thảo" cũng như trực quan "ẩn" nền trên nền trắng. –

+0

Lưu ý rằng nền ở hình chữ nhật. –

+0

thats right @ Lukasz'Severiaan'Grela – Sourabh

70

Giải pháp của tôi với # 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 

Kết quả:

enter image description here

+0

rất tốt đẹp cảm ơn bạn – ashokdy

+0

phạt tiền đối với xcode 7.1 –

+0

Tùy chọn tốt! Đã làm cho tôi. Cảm ơn. – 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 
     } 
    } 
} 

bộ lớp trong Identity thanh tra

enter image description here

thay đổi màu sắc từ Attributes thanh tra

enter image description here

Output

enter image description here

+0

Nó không được đưa ra thích hợp đặt trong swift 3 –

+0

@KetanP bạn có thể giải thích vấn đề chi tiết hơn? –

0

loại c khách quan để sử dụng trên bất kỳ UISlider trong dự án sử dụng mã hoặc bảng phân cảnh:

#import <UIKit/UIKit.h> 

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

thực hiện

#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

Cách tốt nhất để quản lý kích thước màu nền & của UISwitch

Đối với bây giờ nó Swift 2.3 đang

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

cách an toàn khác ở Swift 3 không có giá trị 16pt huyền diệu:

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 dễ nhất và cách nhanh nhất để có được nó trong 3 bước:

// 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 

Nếu bạn tự thay đổi kích thước của công tắc (ví dụ, bằng cách sử dụng autolayout), bạn sẽ phải cập nhật switch.layer.cornerRadius quá, ví dụ, bằng cách ghi đè layoutSubviews và sau khi gọi siêu cập nhật bán kính góc:

override func layoutSubviews() { 
    super.layoutSubviews() 
    switchControl.layer.cornerRadius = integrationSwitch.bounds.height/2 
} 
Các vấn đề liên quan