簡體   English   中英

在Xcode8上自定義UISegmentedControl邊框和背景-Swift 3

[英]Customizing UISegmentedControl border and Background on Xcode8 - Swift 3

我正在嘗試自定義UISegmented控件的外觀,如下圖: 在此處輸入圖片說明

更具體地說,我在嘗試放置陰影,設置高度以及背景顏色時遇到問題。

你能幫我嗎?

您可以通過子類化自定義UISegmentController。 請參見下面的代碼。

class SegmentController : UISegmentedControl {

 override func awakeFromNib() {
    self.layer.borderWidth = 0
    self.tintColor = UIColor.clear
    self.backgroundColor = UIColor.white
    let segAttributesSelected: NSDictionary = [
        NSAttributedStringKey.foregroundColor: UIColor.red,
        NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 15)
    ]
    let segAttributes: NSDictionary = [
        NSAttributedStringKey.foregroundColor: UIColor.lightGray,
        NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 15)
    ]
    self.setTitleTextAttributes(segAttributesSelected as [NSObject : AnyObject], for: UIControlState.selected)
    self.setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.normal)
 }

 override func draw(_ rect: CGRect) {
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowRadius = 2.0
    self.layer.shadowOffset = CGSize.init(width: 0, height: 0)
    self.layer.shadowOpacity = 1.0
    self.layer.backgroundColor = UIColor.white.cgColor
 }
}

對情節提要中的約束進行適當的調整以設置高度和位置。 故事板屏幕短


最終輸出

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM