I have created a simple UIView class that will show a label on runtime.
@IBDesignable
class CalendarDayView: UIView {
var dayLabel = UILabel()
@IBInspectable
var day: Int {
set {
dayLabel.text = String(newValue)
}
get {
return Int(dayLabel.text!)!
}
}
override init(frame: CGRect) {
super.init(frame: frame)
prepareSubviews()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
prepareSubviews()
}
func prepareSubviews() {
dayLabel.backgroundColor = UIColor.blue
addSubview(dayLabel)
dayLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor)
dayLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor)
dayLabel.widthAnchor.constraint(equalTo: self.widthAnchor)
dayLabel.heightAnchor.constraint(equalTo: self.heightAnchor)
setNeedsLayout()
}
}
I have added a UIView on a simple view controller in the story board and set its width and height as 100. In the attribute inspector, the day value is set as 1.
I don't get to see the custom view background color (which should be blue) nor the label (which should show 1). Did I miss out something?
dayLabel
? – BallpointBendayLabel.translatesAutoresizingMaskIntoConstraints = false
– BallpointBenCalendarDayView
? Try putting in a breakpoint and printing out the frame after it has been initialized and added to whatever superview you added it to. – creeperspeak