使用编程 VFL 约束初始化 UIControl (iOS Swift)
Initializing UIControl using programmatically VFL constraints (iOS Swift)
我正在处理几个需要以编程方式创建的自定义 UIControl 对象。通常在使用 AutoLayout 时我只传递一个 CGRect。使用视觉格式语言时:
override init(frame: CGRect) {
super.init(frame: frame)
updateLayer()
}
在 UIControl class 中未被 ViewDidLoad() 中的约束方法调用,并且 CAShapeLayers 未更新。自定义 button/slider/knob 等工作但不可见,我可以在触摸时立即更新形状。不是很有帮助。如何在加载视图后立即在屏幕上显示使用 VFL 的自定义 UIControl?这就是我想要做的:
import UIKit
class ViewController: UIViewController {
let knob = Knob()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(knob)
knob.translatesAutoresizingMaskIntoConstraints = false
let views = [
"slider": knob
]
let formatV = "V:|-[knob(300)]-|"
let formatH = "H:|-[knob(300)]-|"
let VC = NSLayoutConstraint.constraints(withVisualFormat: formatV, options: NSLayoutFormatOptions(), metrics: nil, views: views)
let HC = NSLayoutConstraint.constraints(withVisualFormat: formatH, options: NSLayoutFormatOptions(), metrics: nil, views: views)
self.view.addConstraints(VC)
self.view.addConstraints(HC)
knob.addTarget(self, action: #selector(knobRotated(_:)), for: .touchDown)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func knobRotated(_ sender: Knob) {
print(sender.value)
}
}
或者创建同时适用于 AutoLayout 和 VFL 的 UIControl 的最佳方法是什么?
UIControl
是 UIView
的子类,所以对待它们的方式类似。理解UIView
的生命周期,它有很多方法,是解决问题的关键。从它们内部打印并查看它们何时以及多久发射一次。下面是子类化时使用的更常见的生命周期方法 UIView
.
class CustomButton: UIControl {
override init(frame: CGRect) {
super.init(frame: frame)
print("init")
}
override func updateConstraints() {
print("update constraints")
super.updateConstraints() // in this method, super is to be called last
}
override func layoutSubviews() {
super.layoutSubviews()
print("layout subviews")
}
}
我正在处理几个需要以编程方式创建的自定义 UIControl 对象。通常在使用 AutoLayout 时我只传递一个 CGRect。使用视觉格式语言时:
override init(frame: CGRect) {
super.init(frame: frame)
updateLayer()
}
在 UIControl class 中未被 ViewDidLoad() 中的约束方法调用,并且 CAShapeLayers 未更新。自定义 button/slider/knob 等工作但不可见,我可以在触摸时立即更新形状。不是很有帮助。如何在加载视图后立即在屏幕上显示使用 VFL 的自定义 UIControl?这就是我想要做的:
import UIKit
class ViewController: UIViewController {
let knob = Knob()
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(knob)
knob.translatesAutoresizingMaskIntoConstraints = false
let views = [
"slider": knob
]
let formatV = "V:|-[knob(300)]-|"
let formatH = "H:|-[knob(300)]-|"
let VC = NSLayoutConstraint.constraints(withVisualFormat: formatV, options: NSLayoutFormatOptions(), metrics: nil, views: views)
let HC = NSLayoutConstraint.constraints(withVisualFormat: formatH, options: NSLayoutFormatOptions(), metrics: nil, views: views)
self.view.addConstraints(VC)
self.view.addConstraints(HC)
knob.addTarget(self, action: #selector(knobRotated(_:)), for: .touchDown)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@objc func knobRotated(_ sender: Knob) {
print(sender.value)
}
}
或者创建同时适用于 AutoLayout 和 VFL 的 UIControl 的最佳方法是什么?
UIControl
是 UIView
的子类,所以对待它们的方式类似。理解UIView
的生命周期,它有很多方法,是解决问题的关键。从它们内部打印并查看它们何时以及多久发射一次。下面是子类化时使用的更常见的生命周期方法 UIView
.
class CustomButton: UIControl {
override init(frame: CGRect) {
super.init(frame: frame)
print("init")
}
override func updateConstraints() {
print("update constraints")
super.updateConstraints() // in this method, super is to be called last
}
override func layoutSubviews() {
super.layoutSubviews()
print("layout subviews")
}
}