0
我使用此視頻創建了自定義分段控件(https://www.youtube.com/watch?v=xGdRCUrSu94&t=2502s)。我希望能夠在運行期間將自定義分段控件(commaSeperatedButtonTitles)中顯示的字符串值從1,2,3,4更改爲5,6,7,8,但由於某些原因,視圖不更新值。這些值似乎可以在viewDidLoad中進行更改,但不包含我需要的操作事件。IBDesignable屬性在運行時不更新
import UIKit
@IBDesignable
class CustomSegmentedControl: UIControl {
var buttons = [UIButton]()
var selector: UIView!
var selectedSegmentIndex = 0
@IBInspectable
var borderWidth: CGFloat = 0 {
didSet{
layer.borderWidth = borderWidth
}
}
@IBInspectable
var borderColor: UIColor = UIColor.gray {
didSet{
layer.borderColor = borderColor.cgColor
}
}
@IBInspectable
var commaSeperatedButtonTitles: String = "" {
didSet{
}
}
@IBInspectable
var textColor: UIColor = .lightGray {
didSet{
}
}
@IBInspectable
var selectorColor: UIColor = .blue {
didSet{
}
}
@IBInspectable
var selectorTextColor: UIColor = .white {
didSet{
}
}
func updateView(){
buttons.removeAll()
subviews.forEach { (view) in
view.removeFromSuperview()
}
var buttonTitles = commaSeperatedButtonTitles.components(separatedBy: ",")
for buttonTitle in buttonTitles {
let button = UIButton(type: .system)
button.setTitle(buttonTitle, for: .normal)
button.setTitleColor(textColor, for: .normal)
button.addTarget(self, action: #selector(buttonTapped(button:)), for: .touchUpInside)
buttons.append(button)
}
buttons[0].setTitleColor(selectorTextColor, for: .normal)
let selectorWidth = (frame.width/CGFloat(buttonTitles.count))
selector = UIView(frame: CGRect(x: 0, y: 0, width: selectorWidth, height: frame.height))
selector.layer.cornerRadius = (frame.height/2)
selector.backgroundColor = selectorColor
addSubview(selector)
let sv = UIStackView(arrangedSubviews: buttons)
sv.axis = .horizontal
sv.alignment = .fill
sv.distribution = .fillEqually
addSubview(sv)
sv.translatesAutoresizingMaskIntoConstraints = false
sv.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
sv.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
sv.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
sv.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
self.setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
// Drawing code
layer.cornerRadius = (frame.height/2)
//updateView()
}
override func layoutSubviews() {
updateView()
}
func buttonTapped(button: UIButton){
for (buttonIndex, btn) in buttons.enumerated() {
btn.setTitleColor(textColor, for: .normal)
if btn == button{
selectedSegmentIndex = buttonIndex
let selectorStartPosition = (frame.width/CGFloat(buttons.count) * CGFloat(buttonIndex))
UIView.animate(withDuration: 0.3, animations: {
self.selector.frame.origin.x = selectorStartPosition
})
btn.setTitleColor(selectorTextColor, for: .normal)
}
}
sendActions(for: .valueChanged)
}
}
守則 - 視圖 - 控制器的動作事件中:
customSegment.commaSeperatedButtonTitles = "5,6,7,8"
謝謝,這是一個奇怪的是,它沒有被調用。從layoutSubviews刪除updateView不會影響自定義細分的功能嗎?感謝您的建議 –
只有在您的自定義分段控件初始化後沒有設置'commaSeparatedButtonTitles',刪除'updateView()'調用纔可能影響行爲。所以這取決於你如何使用它。爲了使它更健壯一些,我會在初始化程序或'awakeFromNib()'方法中爲默認值賦予'commaSeparatedButtonTitles'。 –
是有道理的,謝謝你的幫助! –