benim UISegmentedControl
Swift yazılmış:Swift kullanıcı tarafından seçildiğinde UISegmentedControl için stili nasıl değiştirebilirim? Bu
:
let selectedAttributes: NSDictionary = [
NSForegroundColorAttributeName: UIColor.black,
NSFontAttributeName: fontForSegmentedControl!
]
let normalAttributes: NSDictionary = [
NSForegroundColorAttributeName: UIColor.gray,
NSFontAttributeName: fontForSegmentedControl!
]
mySegmentedControl.setTitleTextAttributes(selectedAttributes as [NSObject : AnyObject], for: UIControlState.selected)
mySegmentedControl.setTitleTextAttributes(normalAttributes as [NSObject : AnyObject], for: UIControlState.normal)
ve çerçeveleri kaldırmak için uzatma buradadır:
extension UISegmentedControl {
func removeBorders() {
setBackgroundImage(imageWithColor(color: UIColor.white/*backgroundColor!*/), for: .normal, barMetrics: .default)
setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
setDividerImage(imageWithColor(color: UIColor.clear), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
}
// create a 1x1 image with this color
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
context!.setFillColor(color.cgColor);
context!.fill(rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image!
}
}
Ama bununla ilgili bir sorun var.
Ben ONE
veya TWO
(o kullanıcının parmağının dokunduğu ediliyor kez) bu arka plan rengini değiştirir dokunun (tutun) Ne zaman:
Ben bazı kod eksik UISegmentedControl
'da seçilen (geçici basılı) seçenek için stil değiştirme.
Koyu gri seçimini nasıl kaldırabilirim ve açık renkte bırakabilirim? Zaten UIControlState.normal
için arka plan görüntüsü belirledik Benzer nasıl
gibi değiştirin. :) şimdi kullanıcı şu anda seçili olmayan seçeneği bastığında - gri renkte vurgulanmayacaktır - hala seçili olan seçeneğe basma olasılığı var ve arka plan griye dönüşüyor - bunu nasıl düzeltebilirim ? – user3766930
@ user3766930 emin, yapabilirsin, cevabı güncelledim. –