gpt4 book ai didi

ios - IBDesignable 属性在运行时不更新

转载 作者:行者123 更新时间:2023-11-28 15:22:23 26 4
gpt4 key购买 nike

我使用此视频 (https://www.youtube.com/watch?v=xGdRCUrSu94&t=2502s) 创建了自定义分段控件。我希望能够在运行时将自定义分段控件 (commaSeperatedButtonTitles) 中显示的字符串值从 1,2,3,4 更改为 5,6,7,8,但由于某种原因, View 未更新这些值。这些值似乎可以在 viewDidLoad 中更改,但不能在我需要的 Action 事件中更改。

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)
}


}

View Controller 中的 Action 事件代码:

 customSegment.commaSeperatedButtonTitles = "5,6,7,8"

最佳答案

在我看来,在设置 commaSeperatedButtonTitles 属性后,未调用 updateView() 方法。设置属性后尝试调用它:

@IBInspectable
var commaSeperatedButtonTitles: String = "" {
didSet {
self.updateView()
}
}

还有一点值得一提:可能没有必要每次都在 layoutSubviews() 上调用 updateView(),因为它会为您的自定义分段控件重新创建所有按钮。

关于ios - IBDesignable 属性在运行时不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45774655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com