Pulsator is not visible until I close the app and reopen it (causing viewDidLayoutSubviews to trigger). No matter if I call centerPulsator(), force layouting subviews upon load etc. What could be the problem?
import Foundation
import Pulsator
class TestView: UIViewController {
var icon: UIImage?
var message: String?
var buttonTitle: String?
lazy var iconView: UIImageView = { [unowned self] in
let view = UIImageView()
view.backgroundColor = UIColor.clearColor()
view.image = self.icon
return view
}()
lazy var pulsator: Pulsator = { [unowned self] in
let pulsator = Pulsator()
pulsator.numPulse = 3
pulsator.radius = 100
pulsator.backgroundColor = UIColor.redColor()
pulsator.position = self.iconView.layer.position
return pulsator
}()
lazy var messageLabel: UILabel = { [unowned self] in
let label = UILabel()
label.textColor = UIColor.darkGrayColor()
label.font = UIFont.systemFontOfSize(17)
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 0
label.minimumScaleFactor = 0.5
label.textAlignment = .Center
label.text = self.message
return label
}()
lazy var settings: UIButton = { [unowned self] in
let button = UIButton()
button.backgroundColor = UIColor.redColor()
button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
button.setTitle(self.buttonTitle, forState: .Normal)
button.addTarget(self, action: #selector(openSystemSettings), forControlEvents: .TouchUpInside)
return button
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.whiteColor()
[iconView, messageLabel, settings].forEach {
view.addSubview($0)
$0.translatesAutoresizingMaskIntoConstraints = false
view.bringSubviewToFront($0)
}
iconView.layer.superlayer?.insertSublayer(pulsator, below: iconView.layer)
setupConstraints()
pulsator.start()
centerPulsator()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
centerPulsator()
}
func centerPulsator() {
iconView.layer.layoutIfNeeded()
pulsator.position = iconView.layer.position
}
func openSystemSettings() {
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
func setupConstraints() {
// Icon and pulsator
for attribute: NSLayoutAttribute in [.Width, .Height] {
view.addConstraint(NSLayoutConstraint(item: iconView, attribute: attribute, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 70))
}
for attribute: NSLayoutAttribute in [.CenterX, .CenterY] {
view.addConstraint(NSLayoutConstraint(item: iconView, attribute: attribute, relatedBy: .Equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0))
}
// Settings Button
view.addConstraint(NSLayoutConstraint(item: settings, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 50))
for attribute: NSLayoutAttribute in [.Left, .Right, .Bottom] {
view.addConstraint(NSLayoutConstraint(item: settings, attribute: attribute, relatedBy: .Equal, toItem: view, attribute: attribute, multiplier: 1, constant: 0))
}
// Message Label
view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Bottom, relatedBy: .Equal, toItem: settings, attribute: .Top, multiplier: 1, constant: -40))
view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Left, relatedBy: .Equal, toItem: view, attribute: .Left, multiplier: 1, constant: 20))
view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Right, relatedBy: .Equal, toItem: view, attribute: .Right, multiplier: 1, constant: -20))
}
}