Tôi đã học được rằng chúng ta có thể thay đổi giao diện nút UISwitch ở trạng thái "bật", nhưng cũng có thể thay đổi màu của UISwitch ở trạng thái "tắt" ?Thay đổi màu của UISwitch ở trạng thái "tắt"
Trả lời
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 +
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
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). –
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.
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. –
Lưu ý rằng nền ở hình chữ nhật. –
thats right @ Lukasz'Severiaan'Grela – Sourabh
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ả:
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
thay đổi màu sắc từ Attributes thanh tra
Output
Nó không được đưa ra thích hợp đặt trong swift 3 –
@KetanP bạn có thể giải thích vấn đề chi tiết hơn? –
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
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
}
}
}
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)
}
}
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
}
- 1. Cách gọi hành động khi UISwitch thay đổi trạng thái?
- 2. Cách thay đổi màu nền của thanh trạng thái android
- 3. Màu của UISwitch
- 4. Cách thay đổi màu mặc định UISwitch (màu xanh)
- 5. Thay đổi Nhãn của UISwitch
- 6. Thay đổi màu chữ của thanh trạng thái từ màu trắng iOS 7/Xcode 5
- 7. Làm cách nào để thay đổi màu văn bản của Android ToogleButton về thay đổi trạng thái?
- 8. Tại sao display_errors thay đổi mã trạng thái HTTP?
- 9. Làm cách nào để thay đổi màu dòng trạng thái vim?
- 10. Tắt thay đổi màu của thẻ neo khi truy cập
- 11. màu thẻ trạng thái với ActiveAdmin
- 12. Thay đổi độ mờ trong trạng thái được nhấn của hình ảnh
- 13. Phát hiện Thay đổi trạng thái pin ở mọi tỷ lệ phần trăm trong android
- 14. Kiểm soát trạng thái bật/tắt của nút lưu theo chương trình
- 15. Làm thế nào để thay đổi màu sắc của hud nơi màu xám
- 16. Trạng thái Voiceover của iOS
- 17. Thay đổi trạng thái Lync theo tập lệnh
- 18. Thay đổi thuộc tính thanh trạng thái ứng dụng iPhone
- 19. Thay đổi trạng thái bị trì hoãn UIButton
- 20. jQuery Mobile - Thay đổi trạng thái radiobutton lập trình
- 21. Bảng phân cảnh iOS: Màu thanh trạng thái
- 22. Flex 4: Sự kiện thay đổi trạng thái
- 23. Xem sự thay đổi trạng thái địa chỉ IP
- 24. Thay đổi vim của dòng trạng thái của "tập tin sửa đổi" cờ để được chú ý hơn
- 25. ASM: Chuyển đổi trạng thái
- 26. PHP - trạng thái chuyển đổi
- 27. Làm thế nào để bạn thay đổi trạng thái của nút đóng cửa sổ trong Cocoa?
- 28. Ajax và nút quay lại. Thay đổi băm, nhưng trạng thái trang trước đó được lưu trữ ở đâu?
- 29. Tại sao ColorAnimation cho trạng thái được kiểm tra không bền màu sau khi trạng thái MouseOver được kích hoạt?
- 30. Không thể thay đổi trạng thái hiện diện của người dùng của tôi bằng Smack
bạn đã sử dụng thuộc tính tintColor để thay đổi màu? – rishi