MapKit iOS 9 detailCalloutAccessoryView usage

后端 未结 4 1008
遥遥无期
遥遥无期 2020-12-05 05:39

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

4条回答
  •  有刺的猬
    2020-12-05 05:53

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

提交回复
热议问题