After watching WWDC video 206 I assumed this would be a trivial task of adding the detail callout view to a mapView annotation view.
So, I assume Im doing something
You can use a custom class and override intrinsicContentSize
to dynamically size the detail view dependent on its childrens content. (as hinted at by @Klaas in the accepted answer)
This may be especially helpful, if the size of your view is not known beforehand or does change during runtime or you simply do not want to add constraints programmatically.
Here is an example using two labels which are inside a stack view. The intrinsicContentSize
is set to equal the size the stack view would take. Setting an instance of the following as the detailAccessoryView
should get you a view that reacts to change in the labels text and needs no programmatically added constraints.
class CustomCalloutDetailView : UIView {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var label2: UILabel!
@IBOutlet weak var mainStack: UIStackView!
override var intrinsicContentSize: CGSize {
get {
return mainStack.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
}
}
public func setLabel1(_ labelText: String) {
self.label1.text = labelText
self.invalidateIntrinsicContentSize()
}
}