2017-02-23 16 views
11

benim UISegmentedControl Swift yazılmış:Swift kullanıcı tarafından seçildiğinde UISegmentedControl için stili nasıl değiştirebilirim? Bu

Aşağıdaki kod ile onu oluşturan

enter image description here

:

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:

enter image description here

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

cevap

9

, sen UIControlState.highlighted ile + UIControlState.highlightedUIControlState.selected için uygun görüntüleri ayarlamak gerekir.

setBackgroundImage(imageWithColor(color: .clear), for: .highlighted, barMetrics: .default) 
setBackgroundImage(imageWithColor(color: tintColor!), for: [.selected, .highlighted], barMetrics: .default) 
+0

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

+1

@ user3766930 emin, yapabilirsin, cevabı güncelledim. –

0
let normalAttributes: NSDictionary = [ 
    NSForegroundColorAttributeName: UIColor.gray, 
    NSFontAttributeName: fontForSegmentedControl! 
] 

mySegmentedControl.setTitleTextAttributes(selectedAttributes as [NSObject : AnyObject], for: UIControlState.selected) 

:

Uzantınızın fonksiyonu removeBorders() aşağıdaki kodu ekleyin (tuşuna basmamış seçilen segment için net bir arka plan ve bir tane seçilmiş sıkıştırılıp bir renk tonu renkli arka plan istiyorum varsayarak) Yukarıdaki kod sadece seçim için gri ekler, bu yüzden onu

let normalAttributes: NSDictionary = [ 
    NSForegroundColorAttributeName: UIColor.white, 
    NSFontAttributeName: fontForSegmentedControl! 
] 

mySegmentedControl.setTitleTextAttributes(selectedAttributes as [NSObject : AnyObject], for: UIControlState.selected) 
İlgili konular